根据url地址生成静态页面

最近在做一个新闻系统,前台新闻页面要生成HTML静态页面,
自己找了一些资料,终于达到预期效果
现分享如下:
None.gif using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Net;
None.gif
using  System.Configuration;
None.gif
None.gif
private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Encoding code 
= Encoding.GetEncoding("utf-8");
InBlock.gif            StreamReader sr 
= null;
InBlock.gif            StreamWriter sw 
= null;
InBlock.gif            
string str = null;
InBlock.gif
InBlock.gif            
//读取远程路径
InBlock.gif
            WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
InBlock.gif            WebResponse myTemp 
= temp.GetResponse();
InBlock.gif            sr 
= new StreamReader(myTemp.GetResponseStream(), code);
InBlock.gif            
//读取
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sr 
= new StreamReader(myTemp.GetResponseStream(), code);
InBlock.gif                str 
= sr.ReadToEnd();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sr.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string   path   =   HttpContext.Current.Server.MapPath("../news");//取得新闻当前文件夹 
InBlock.gif
            DateTime   year   =   DateTime.Now;   
InBlock.gif            
string   years   =   Convert.ToString(year.Year);//当前年   
InBlock.gif
            string   month   =   Convert.ToString(year.Month);//当前月   
InBlock.gif
            string   CurrentPath   =   path   +   "/"   +   years;   //设置当前年目录   
InBlock.gif
                     if(Directory.Exists(CurrentPath)==false)   //若该目录不存在,创建该目录   
InBlock.gif
                  Directory.CreateDirectory(CurrentPath);   
InBlock.gif            
string   CurrentMonthPath   =   CurrentPath   +   "/"   +   month;//设置当前月目录   
InBlock.gif
            if(Directory.Exists(CurrentMonthPath)==false)   //若该目录不存在,创建该目录   
InBlock.gif
                  Directory.CreateDirectory(CurrentMonthPath);   
InBlock.gif            
InBlock.gif            
string fileName = DateTime.Now.ToString("ddHHmmss"+ ".htm";
InBlock.gif
InBlock.gif            
//写入
InBlock.gif
            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sw 
= new StreamWriter(CurrentMonthPath+"/" + fileName, false, code);
InBlock.gif                sw.Write(str);
InBlock.gif                sw.Flush();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sw.Close();
InBlock.gif                Response.Write(
"恭喜<a href="+CurrentMonthPath+"/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
InBlock.gif                Response.Write(CurrentMonthPath
+"/"+fileName);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/mickey/archive/2007/06/26/796146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值