以下为引用的内容: using System; using System.IO; using System.Web; using System.Text; namespace PowerLeader.Components ...{ /**//// <summary> /// WriteTOHtml 的摘要说明。 /// </summary> public class WriteTOHtml ...{ public WriteTOHtml() ...{ // // TODO: 在此处添加构造函数逻辑 // } public static void WriteNews(int id) ...{ News news = new News(); News.NewsDetails newsDetails = new PowerLeader.Components.News.NewsDetails(); newsDetails = news.GetNews(id); bool flag; flag = WriteFile(newsDetails); } public static bool WriteFile(News.NewsDetails newsDetails) ...{ Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/PowerLeader/html/" newsDetails.addtime.ToString("yyyy") "/" newsDetails.addtime.ToString("MM"))); string path = HttpContext.Current.Server.MapPath("../html/" newsDetails.addtime.ToString("yyyy") "/" newsDetails.addtime.ToString("MM") "/"); Encoding code = Encoding.GetEncoding("gb2312"); // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("../html/text.html"); StreamReader sr = null; StreamWriter sw = null; string stringTempCode = ""; try ...{ sr = new StreamReader(temp, code); stringTempCode = sr.ReadToEnd(); // 读取文件 } catch(Exception exp) ...{ HttpContext.Current.Response.Write(exp.Message); HttpContext.Current.Response.End(); sr.Close(); } string htmlFileName = newsDetails.addtime.ToString("yyyyMMddHHmmss") ".html"; // 替换内容 // 这时,模板文件已经读入到名称为str的变量中了 stringTempCode = stringTempCode.Replace("$PageTitle$","抗战OnLine官方网站..."); stringTempCode = stringTempCode.Replace("$Type$",newsDetails.type.ToString().Trim()); stringTempCode = stringTempCode.Replace("$Author$",newsDetails.author.ToString().Trim()); stringTempCode = stringTempCode.Replace("$From$",newsDetails.from.Trim()); stringTempCode = stringTempCode.Replace("$Time$",newsDetails.addtime.ToString().Trim()); stringTempCode = stringTempCode.Replace("$Title$",newsDetails.title.Trim()); stringTempCode = stringTempCode.Replace("$Content$",newsDetails.content); // 写文件 try ...{ sw = new StreamWriter(path htmlFileName , false, code); sw.Write(stringTempCode); sw.Flush(); } catch(Exception ex) ...{ HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); } finally ...{ sw.Close(); } return true; } } } |