// Generate an HTML page
Public static bool writefile (string strtext, string strcontent, string strauthor)
{
String Path = httpcontext. Current. server. mappath ("/news /");
Encoding code = encoding. getencoding ("gb2312 ");
// Read the Template File
String temp = httpcontext. Current. server. mappath ("/news/text.html ");
Streamreader sr = NULL;
Streamwriter Sw = NULL;
String STR = "";
Try
{
Sr = new streamreader (temp, Code );
STR = Sr. readtoend (); // read the file
}
Catch (exception exp)
{
Httpcontext. Current. response. Write (exp. Message );
Httpcontext. Current. response. End ();
Sr. Close ();
}
String htmlfilename = datetime. Now. tostring ("yyyymmddhhmmss") + ". html ";
// Replace content
// At this time, the template file has been read into the variable named STR
STR = Str. Replace ("showarticle", strtext); // showarticle on the template page
STR = Str. Replace ("biaoti", strtext );
STR = Str. Replace ("content", strcontent );
STR = Str. Replace ("author", strauthor );
// Write an object
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 ();
}
Return true;
This function is stored in the conn. CS base class.
Reference in the Code for adding news: Project name: hover
If (hover. Conn. writefilethis. Title. Text. tostring), this. content. Text. tostring), this. Author. Text. tostring )))
{
Response. Write ("added successfully ");
}
Else
{
Response. Write ("An error occurred while generating HTML! ");
}
Template page text.html code
Code:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> showarticle </title>
<Body>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html>
A prompt is displayed, indicating that an HTML file with the current time as the file name is displayed! The above only writes the passed parameters to the HTML file. In actual applications, you need to add the database before writing the HTML file.