asp.net批量生成静态网页,四

protected void Button1_Click(object sender, EventArgs e)

{

for(int i=Convert.ToInt32(this.TextBox1.Text);i<=Convert.ToInt32(this.TextBox2.Text);i++)

this.Createhtml(i.ToString());

string[] format = new string[1];//定义和htmlyem标记数目一致的数组

StringBuilder htmltext = new StringBuilder();

try

{

using (StreamReader sr = new StreamReader(Server.MapPath("index_tmp.htm")))

{

String line;

while ((line = sr.ReadLine()) != null)

{

htmltext.Append(line);

}

sr.Close();

}

}

catch

{

Response.Write("<Script>alert('读取文件错误')</Script>");

}

////---------------------给标记数组赋值------------  

format[0] = forindex;

////----------替换htm里的标记为你想加的内容

htmltext.Replace("$htmlformat[0]", format[0]);

//----------生成htm文件------------------――   

try

{

using (StreamWriter sw = new StreamWriter(Server.MapPath("index.html"), false, System.Text.Encoding.GetEncoding("GB2312")))

{

sw.WriteLine(htmltext);

sw.Flush();

sw.Close();

}

}

catch

{

Response.Write("The file could not be wirte:");

}

Response.Redirect("index.html");

}

}

本程序生成页面的速度很快,生成800个页面只需要4秒钟左右。但是方法不是很好,代码还不够简明,希望广大朋友们提出意见。如果你对这方法有什么改进的办法,也不妨拿出来大家分享一下。