Using System. Text;
Using System. IO;
String mbPath = Server. MapPath ("template.htm ");
Encoding code = Encoding. GetEncoding ("gb2312 ");
StreamReader sr = null;
StreamWriter sw = null;
String str = null;
// Read
Try
{
Sr = new StreamReader (mbPath, code );
Str = sr. ReadToEnd ();
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}
// Automatically rename the file according to the time, and the extension can be modified by yourself
String fileName = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". htm ";
Str = str. Replace ("$ title $", txtTitle. Text); // Replace the Title
Str = str. Replace ("$ content $", txtContent. Text); // Replace content
// Generate a file
Try
{
Sw = new StreamWriter (Server. MapPath ("htm/") + fileName, false, code );
Sw. Write (str );
Sw. Flush ();
}
Catch (Exception ex)
{
Throw ex;
}
Finally
{
Sw. Close ();
Response. Write ("<a href = htm/" + fileName + "target = _ blank>" + fileName + "</a> generated! ");
}