网页静态化,生成静态文件的类 using System; using System.IO; using System.Net; using System.Web; namespace ClassLibrary.DataClass { /// <summary> /// GenHtml 的摘要说明。 /// </summary> public class GenHtml { public static string path=""; public static string fileName=""; public static string transUrl=""; public GenHtml() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 转换成静态文件 /// </summary> /// <param name="path">要写入静态文件的真实路径</param> /// <param name="fileName">真实的文件名</param> /// <param name="transUrl">要转换的地址</param> public static bool writeFile(string path, string fileName, string transUrl) { //是否正确生成 bool bSuccess = false; StreamReader sr; StreamWriter sw; try { WebRequest HttpWebRequest = WebRequest.Create(transUrl); WebResponse HttpWebResponse = HttpWebRequest.GetResponse(); sr = new StreamReader(HttpWebResponse.GetResponseStream()); string strHtml = sr.ReadToEnd(); sr.Close(); if(!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } string PathAndName = path + fileName; if (System.IO.File.Exists(PathAndName)) { System.IO.File.Delete(PathAndName); } sw = File.CreateText(PathAndName); sw.WriteLine(strHtml); sw.Close(); //生成成功 bSuccess = true; } catch { } return bSuccess; } public void threadWriteFile() { StreamReader sr; StreamWriter sw; try { WebRequest HttpWebRequest = WebRequest.Create(transUrl); WebResponse HttpWebResponse = HttpWebRequest.GetResponse(); sr = new StreamReader(HttpWebResponse.GetResponseStream()); string strHtml = sr.ReadToEnd(); sr.Close(); if(!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } string PathAndName = path + fileName; if (System.IO.File.Exists(PathAndName)) { System.IO.File.Delete(PathAndName); } sw = File.CreateText(PathAndName); sw.WriteLine(strHtml); sw.Close(); } catch { } } /// <summary> /// 删除生成文件 /// </summary> /// <param name="path">文件真实路径</param> /// <param name="fileName">文件名</param> /// <returns>布尔值</returns> public static bool delFile(string path,string fileName) { //是否成功的标志 bool bSuccess=false; try { string pathAndName=path+fileName; if(System.IO.File.Exists(pathAndName)) { System.IO.File.Delete(pathAndName); bSuccess=true; } } catch { } return bSuccess; } } } |
|