【IT168技术文档】
现在做程序都要将动态的也面转换成静态页面,今天教大家在ASP。NET 中实现静态页面的生成方法。
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Text; /// <summary> /// WriteFile 的摘要说明 /// </summary> public class WriteFile { public WriteFile() { } public static bool createHtml(string[] strnewsHtml,string[] stroldHtml,string strModeFilePath,string strPath) { bool flag = false; StreamReader sr = null; StreamWriter sw = null; string filepath = HttpContext.Current.Server.MapPath(strModeFilePath); Encoding code = Encoding.GetEncoding("gb2312"); string s = string.Empty; try { sr = new StreamReader(filepath,code); s = sr.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { sr.Close(); } try { for (int i = 0; i < strnewsHtml.Length; i++) { s = s.Replace(stroldHtml[i], strnewsHtml[i]); } sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code); sw.Write(s); flag = true; } catch (Exception ex) { flag = false; throw ex; } finally { sw.Flush(); sw.Close(); } return flag; } public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml) { bool Flage = false; StreamReader ReaderFile = null; StreamWriter WrirteFile = null; string FilePath = HttpContext.Current.Server.MapPath(strHtml); Encoding Code = Encoding.GetEncoding("gb2312"); string strFile = string.Empty; try { ReaderFile = new StreamReader(FilePath, Code);