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;
    }

}

你可能感兴趣的:(html,String,null,url,asp.net,Path)