
using System;

using System.Data;

using System.Configuration;

using System.Collections;

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;

public partial class _Default : System.Web.UI.Page


...{

protected void Page_Load(object sender, EventArgs e)


...{

WriteFile("奧運專題", "奧運會真棒!<br/>", "2", "2.Html");

}

public void WriteFile(string strText, string content, string page, string name)


...{

string path = HttpContext.Current.Server.MapPath("news/get");

System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");

// 讀取模板文件

string temp = HttpContext.Current.Server.MapPath("news/text.htm");

StreamReader sr = null;

StreamWriter sw = null;

string str = "";

try


...{

sr = new StreamReader(temp, code);

str = sr.ReadToEnd(); // 讀取文件

}

catch (Exception exp)


...{

HttpContext.Current.Response.Write(exp.Message);

HttpContext.Current.Response.End();

sr.Close();

}

string Htmlfilename = name.ToString();

// 替換內容

// 這時,模板文件已經讀入到名稱為str的變量中了

str = str.Replace("$title", strText); //模板頁中的$title,即標題

str = str.Replace("$content", content);//模板頁中的$content,即內容

str = str.Replace("$page", page);//模板頁中的$page,即頁碼連接格式

// 寫文件

try


...{

sw = new StreamWriter(path + "/" + Htmlfilename, false, code);

sw.Write(str);

sw.Flush();

}

catch (Exception ex)


...{

HttpContext.Current.Response.Write(ex.Message);

HttpContext.Current.Response.End();

}

finally


...{

sw.Close();

}

}

}
