一个自动生成html的类

该博客介绍了使用C#实现自动读取模板文件,生成当前年/月/日/yyMMddhhmmss.html文件的功能,并给出了代码来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以自动读取模板文件后,生成   当前 年/月/日/yyMMddhhmmss.html

代码如下

C#

 1 None.gif private   bool   CreateHtml( string  strText, string  strContent, string  strAuthor)
 2 ExpandedBlockStart.gifContractedBlock.gif   dot.gif {
 3InBlock.gif   string yearString=DateTime.Now.Year.ToString();
 4InBlock.gif   string monthString=DateTime.Now.Month.ToString();
 5InBlock.gif   string dayString=DateTime.Now.Day.ToString();
 6InBlock.gif
 7InBlock.gif   string fileName=yearString+monthString+dayString+DateTime.Now.Millisecond.ToString()+".html";
 8InBlock.gif   Directory.CreateDirectory(Server.MapPath(""+yearString+"//"+monthString+"//"+dayString+""));
 9InBlock.gif  
10InBlock.gif   Encoding code=Encoding.GetEncoding("gb2312");
11InBlock.gif   string temp=Server.MapPath("text.html");
12InBlock.gif   StreamReader sr=null;
13InBlock.gif   StreamWriter sw=null;
14InBlock.gif   string str="";
15InBlock.gif   //读取模板
16InBlock.gif   try
17ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
18InBlock.gif    sr=new StreamReader(temp,code);
19InBlock.gif    str=sr.ReadToEnd();
20ExpandedSubBlockEnd.gif   }

21InBlock.gif   catch(Exception exp)
22ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
23InBlock.gif    Response.Write(exp.Message);
24InBlock.gif    Response.End();
25InBlock.gif    sr.Close();
26ExpandedSubBlockEnd.gif   }

27InBlock.gif   //替换
28InBlock.gif   str=str.Replace("ShowArticle",strText);
29InBlock.gif   str=str.Replace("biaoti",strText);
30InBlock.gif   str=str.Replace("content",strContent);
31InBlock.gif   str=str.Replace("author",strAuthor);
32InBlock.gif   
33InBlock.gif   try
34ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
35InBlock.gif
36InBlock.gif    sw=new StreamWriter(Server.MapPath(""+yearString+"//"+monthString+"//"+dayString+"//"+fileName+""),false,code);
37InBlock.gif    sw.Write(str);
38InBlock.gif    sw.Flush();
39ExpandedSubBlockEnd.gif   }

40InBlock.gif   catch(Exception exp)
41ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
42InBlock.gif    HttpContext.Current.Response.Write(exp.Message);
43InBlock.gif    HttpContext.Current.Response.End();
44ExpandedSubBlockEnd.gif   }

45InBlock.gif   finally
46ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
47InBlock.gif    sw.Close();
48ExpandedSubBlockEnd.gif   }

49InBlock.gif   return true;
50InBlock.gif
51ExpandedBlockEnd.gif  }

52 None.gif
 html页代码
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< html >
 
< head >
  
< title > ShowArticle </ title >

 
</ head >
 
< body >
  biaoti
  
< br >
  content
< br >
  author
 
</ body >
</ html >

转载于:https://www.cnblogs.com/zhangchenliang/archive/2007/07/15/819128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值