public void CreateHtml(string path)
{
try
{
string fileName = path + ".html";
if (File.Exists(fileName))
File.Create(fileName);
using (StreamWriter sw = new StreamWriter(fileName, false))
{
sw.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
sw.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
sw.WriteLine("<head>");
sw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
sw.WriteLine("<title> </title>");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<img src=" + path + ">");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
sw.Close();
}
}
catch
{
}
}
{
try
{
string fileName = path + ".html";
if (File.Exists(fileName))
File.Create(fileName);
using (StreamWriter sw = new StreamWriter(fileName, false))
{
sw.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
sw.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
sw.WriteLine("<head>");
sw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
sw.WriteLine("<title> </title>");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<img src=" + path + ">");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
sw.Close();
}
}
catch
{
}
}
这篇博客介绍了如何使用C#创建HTML文件。通过一个名为CreateHtml的方法,生成包含DOCTYPE声明、XHTML标签、元信息、标题以及图片的HTML文件。文件以UTF-8编码,并使用StreamWriter进行写入。
348

被折叠的 条评论
为什么被折叠?



