一、創建一個控制台應用程序
二、創建一個HTML文件夾用來存放HTML
三、代碼實現
static void Main(string[] args) { //當前應用程序集的執行的上級目錄 string dir = Path.GetFullPath("../../"); string path = dir + "HTML"; string script = ""; string content = "<h1>我是一個h1</h1>"; string head = "<head>" + "<meta charset='UTF-8'>" + "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" + "<title>測試頁面</title>" + script + "</head>"; string body = "<body>" + content + "</body>"; string html = "<!DOCTYPE html>" + "<html lang='en'>" + head + body + "</html>"; Logmsg(html, "", path); } /// <summary> /// 創建HTML /// </summary> /// <param name="html">內容</param> /// <param name="title">標題</param> /// <param name="path">路徑</param> public static void Logmsg(string html, string title, string path) { #region 創建HTMLif (Directory.Exists(path) == false)//如果不存在就創建file文件夾 { Directory.CreateDirectory(path); } FileStream fs1 = new FileStream(path + "/" + title + DateTime.Now.ToString("yyyyMMddhhmmss") + ".html", FileMode.Create, FileAccess.Write);//創建寫入文件 StreamWriter sw = new StreamWriter(fs1); sw.WriteLine(html);//開始寫入值 sw.Close(); fs1.Close(); #endregion }
四、效果展示