.net 生成静态页面

 上班无聊时写的,可能还有很多方面考虑到,希望各位园友多多指教!

  1 using  System;
  2 using  System.Data;
  3 using  System.Text;
  4 using  System.Configuration;
  5 using  System.IO;
  6 using  System.Web;
  7 using  System.Web.Security;
  8 using  System.Web.UI;
  9 using  System.Web.UI.WebControls;
 10 using  System.Web.UI.WebControls.WebParts;
 11 using  System.Web.UI.HtmlControls;
 12
 13 ExpandedBlockStart.gifContractedBlock.gif /**/ /// <summary>
 14/// Class1 的摘要说明
 15/// </summary>

 16 public   class  Tohtml
 17 ExpandedBlockStart.gifContractedBlock.gif {
 18 private string FileName;
 19 public string SavePath;
 20 public string TemplateFilePath;
 21 public string ExecSite;
 22
 23 public Tohtml()
 24ExpandedSubBlockStart.gifContractedSubBlock.gif {
 25  //
 26  // TODO: 在此处添加构造函数逻辑
 27  //
 28 }

 29
 30 public Tohtml(string SavePath,string TemplateFilePath)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif {
 32  this.SavePath = SavePath;
 33  this.TemplateFilePath = TemplateFilePath;
 34 }

 35
 36ContractedSubBlock.gifExpandedSubBlockStart.gif 文件名类型#region 文件名类型
 37 public int FileNameType
 38ExpandedSubBlockStart.gifContractedSubBlock.gif {
 39  get 
 40ExpandedSubBlockStart.gifContractedSubBlock.gif  {
 41   FileNameType = 0;
 42   return FileNameType; 
 43  }

 44  set
 45ExpandedSubBlockStart.gifContractedSubBlock.gif  {
 46   //文件存储类型为1时,文件名为日期
 47   if (value == 1)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif   {
 49    this.FileName = DateTime.Now.ToString("yyyyMMddhhmmss"+ ".html";
 50   }

 51   //文件存储类型为2时,文件名为时间
 52   else if (value == 2)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif   {
 54    this.FileName = DateTime.Now.ToString("hhmmss"+ ".html";
 55   }

 56   //文件存储类型为3时,文件名为随机数
 57   else if (value == 3)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif   {
 59    Random Rand = new Random((int)DateTime.Now.Ticks);
 60    this.FileName = Rand.Next().ToString() + ".html";
 61   }

 62   else
 63ExpandedSubBlockStart.gifContractedSubBlock.gif   {
 64    this.FileName = "";
 65   }

 66  }

 67 }

 68 #endregion

 69
 70ContractedSubBlock.gifExpandedSubBlockStart.gif 利用模板创建静态页面#region 利用模板创建静态页面
 71ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
 72 /// 利用模板创建静态页面
 73 /// </summary>
 74 /// <param name="Arg">被替换变量</param>
 75 /// <param name="ArgValue">替换值</param>
 76 /// <returns></returns>

 77 public bool CreateHtmlByTemplate(string []Arg , string []ArgValue)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif {
 79  //判断参数列表是否一致
 80  if (Arg.Length != ArgValue.Length)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif  {
 82   HttpContext.Current.Response.Write("参数列表不一致");
 83   return false;
 84  }

 85  else
 86ExpandedSubBlockStart.gifContractedSubBlock.gif  {
 87
 88   string dir = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + this.SavePath;
 89   if (!System.IO.Directory.Exists(dir))
 90ExpandedSubBlockStart.gifContractedSubBlock.gif   {
 91    System.IO.Directory.CreateDirectory(dir);
 92   }

 93   StringBuilder strHtml = new StringBuilder();
 94
 95   StreamReader sr = new StreamReader(TemplateFilePath,System.Text.Encoding.GetEncoding("gb2312"));
 96   string oneline;
 97
 98   while ((oneline = sr.ReadLine()) != null)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif   {
100    strHtml.Append(oneline);
101   }

102   sr.Close();
103   for (int i = 0; i < Arg.Length; i++)
104ExpandedSubBlockStart.gifContractedSubBlock.gif   {
105    strHtml.Replace(Arg[i], ArgValue[i]);
106   }

107   try
108ExpandedSubBlockStart.gifContractedSubBlock.gif   {
109    //写文件
110    FileInfo fileInfo = new FileInfo(dir + "\\" + this.FileName);
111    FileStream fs = fileInfo.OpenWrite();
112    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
113    //把新的内容写到创建的HTML页面中
114    sw.WriteLine(strHtml);
115    sw.Flush();
116    sw.Close(); 
117   }

118   catch (Exception ex)
119ExpandedSubBlockStart.gifContractedSubBlock.gif   {
120    HttpContext.Current.Response.Write("参数列表不一致");
121   }

122   return true;
123  }

124 }

125 #endregion

126
127ContractedSubBlock.gifExpandedSubBlockStart.gif 执行某个aspx页面后再产生静态页面#region 执行某个aspx页面后再产生静态页面
128ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
129 /// 执行某个aspx页面后再产生静态页面
130 /// </summary>
131 /// <returns></returns>

132 public bool CreateHtmlByExec()
133ExpandedSubBlockStart.gifContractedSubBlock.gif {
134  //调用Main_Execute,并且获取其输出
135  StringWriter sw = new StringWriter();
136  HttpContext.Current.Server.Execute(ExecSite, sw);
137
138  string content = sw.ToString();
139  string filename = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + this.SavePath + "\\" + FileName;
140  //写进文件
141  try
142ExpandedSubBlockStart.gifContractedSubBlock.gif  {
143   using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
144ExpandedSubBlockStart.gifContractedSubBlock.gif   {
145    using (StreamWriter streamwriter = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))
146ExpandedSubBlockStart.gifContractedSubBlock.gif    {
147     streamwriter.Write(content);
148    }

149   }

150   return true;
151  }

152  catch (Exception ex)
153ExpandedSubBlockStart.gifContractedSubBlock.gif  {
154   HttpContext.Current.Response.Write(ex.Message);
155   return false;
156  }

157
158 }

159 #endregion

160
161}

162

转载于:https://www.cnblogs.com/coolkiss/archive/2008/09/24/1298222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值