C#生成静态页面的方法

C#生成静态页面的方法

以下是C#生成静态页面的一种方法

   private bool CreateHl(string strtext, string strcontent, string author)
{
string yearstring = DateTime.Now.Year.ToString();
string monthstring = DateTime.Now.Month.ToString();
string daystring = DateTime.Now.Day.ToString();
string filename = yearstring + monthstring + daystring + DateTime.Now.Millisecond.ToString() + ".html";
Directory.CreateDirectory(Server.MapPath("" + yearstring + "//" + monthstring + "//" + daystring + ""));
Encoding code = Encoding.GetEncoding("gb2312");
string temp = Server.MapPath("test.htm");
Response.Write(temp);
//Response.End();
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd();
}
catch (Exception exp)
{
Response.Write(exp.Message);
Response.End();
sr.Close();
}
strcontent += strcontent.Replace("\n", "<br>");
strcontent += strcontent.Replace("\r", "<br>");
str = str.Replace("{pagetitle}", strtext);
str = str.Replace("{title}", strtext);
str = str.Replace("{content}", strcontent);
str = str.Replace("{author}", author);
try
{
//sw=new StreamWriter(str,code);
// sw.WriteLine
sw = new StreamWriter(Server.MapPath("" + yearstring + "//" + monthstring + "//" + daystring + "//" + filename + ""), false, code);
//sw.Write;
sw.Write(str);
sw.Flush();
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp);
HttpContext.Current.Response.End();

        }
finally
{
sw.Close();
}
msg.Text = filename;
return true;
}

 

 

以下是用法protected void Button2_Click(object sender, EventArgs e)
{
string title = txttitle.Text;
string content = txtcontent.Text;
string zuozhe = txtzuzhe.Text;
bool sc;
sc = CreateHl(title, content, zuozhe);
if (sc)
{
Response.Write("HTML生成成功!");

}
else
{
Response.Write("HTML生成失败!");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值