ASP.NET 生成静态html页

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;
using  System.Data.SqlClient;

/// <summary>
/// CreateHtml 的摘要说明
/// </summary>

public   class  CreateHtml
{
 
public CreateHtml()
 
{
  
//
  
// TODO: 在此处添加构造函数逻辑
  
//
 }

    
    
    
public static bool WriteFile(string title, string nfrom, string publishtime, string publisher, string contont)
    
{
        
// //以下定义     
        
//获取当前Web目录
        
//模版文件
        
//格式化文件名
        
//用于生成年份目录
        
//用于生成月份目录
        
//从temple.html读取得字符

        
string path = HttpContext.Current.Server.MapPath("."+ "/news/";
        
string temp = path + "temple.html";
        
string filename = DateTime.Now.ToString("yyyyMMddHHmmss"+ ".html";
        
string YYYYpath = path + DateTime.Now.Year.ToString();
        
string MMpath=YYYYpath +"/"+DateTime.Now.Month.ToString();
        
string str = "";

        StreamReader sr 
= null;
        StreamWriter sw 
= null;
        Encoding code 
= Encoding.GetEncoding("gb2312");
        
try
        
{
            
//read file
            sr = new StreamReader(temp, code);
            str 
= sr.ReadToEnd();

        }

        
catch (Exception ex)
        
{
            HttpContext.Current.Response.Write(
"读取错误" + ex.Message);
            HttpContext.Current.Response.End();
        }

        
finally
        
{
            sr.Close();
        }


        
//replace 
        str=str.Replace("{$title}", title);
        str 
= str.Replace("{$nfrom}", nfrom);
        str 
= str.Replace("{$publishtime}", publishtime);
        str
=str.Replace("{$publisher}", publisher);
        str
=str.Replace("{$contont}", contont);

        
//write 
        try
        
{
            
//if Directory not Exists else Create Directory;
            if (!Directory.Exists(YYYYpath))
            
{
                Directory.CreateDirectory(YYYYpath);

                
if (!Directory.Exists(MMpath))
                
{
                    Directory.CreateDirectory(MMpath);
                }

            }

            sw 
= new StreamWriter(MMpath +"/"+ filename, false, code);
            sw.Write(str);
            sw.Flush();
        }

        
catch (Exception ex)
        
{
            HttpContext.Current.Response.Write(
"写入失败" + ex.Message);
            HttpContext.Current.Response.End();
        }

        
finally
        
{
            sw.Close();
        }

        
//update myurl
        StringBuilder url = new StringBuilder();
        url.Append(
"news/");
        url.Append(DateTime.Now.Year.ToString());
        url.Append(
"/");
        url.Append(DateTime.Now.Month.ToString());
        url.Append(
"/");
        url.Append(filename);
        SqlConnection conn 
= null;
        
try
        
{
            conn
=Db.createConnection();//从Db的静态方法得到SqlConnection
            conn.Open();
            SqlCommand cmd 
= new SqlCommand("update news set myurl='" + url + "'",conn);
            
if (1 == cmd.ExecuteNonQuery())
            
{
                HttpContext.Current.Response.Write(
"成功插入url<br/>");
            }

        }

        
catch (Exception ex)
        
{
            HttpContext.Current.Response.Write(
"更新url错误" + ex.Message);
            HttpContext.Current.Response.End();
        }

        
finally
        
{
            conn.Close();
        }

        
return true;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值