 public static bool writefile(string strtext,string strcontent,string strauthor)
  public static bool writefile(string strtext,string strcontent,string strauthor) 
 
   {
{ string path = httpcontext.current.server.mappath("/news/");
   string path = httpcontext.current.server.mappath("/news/"); encoding code = encoding.getencoding("gb2312");
   encoding code = encoding.getencoding("gb2312"); // 讀取模板文件
   // 讀取模板文件 string temp = httpcontext.current.server.mappath("/news/text.html");
   string temp = httpcontext.current.server.mappath("/news/text.html"); streamreader sr=null;
   streamreader sr=null; streamwriter sw=null;
   streamwriter sw=null; string str="";
   string str="";   try
   try
 
    {
{ sr = new streamreader(temp, code);
    sr = new streamreader(temp, code); str = sr.readtoend(); // 讀取文件
    str = sr.readtoend(); // 讀取文件 }
   } catch(exception exp)
   catch(exception exp)
 
    {
{ httpcontext.current.response.write(exp.message);
    httpcontext.current.response.write(exp.message); httpcontext.current.response.end();
    httpcontext.current.response.end(); sr.close();
    sr.close(); }
   } 
   
    string htmlfilename=datetime.now.tostring("yyyymmddhhmmss")+".html";
   string htmlfilename=datetime.now.tostring("yyyymmddhhmmss")+".html"; // 替換內容
   // 替換內容 // 這時,模板文件已經讀入到名稱為str的變量中了
   // 這時,模板文件已經讀入到名稱為str的變量中了 str =str.replace("showarticle",strtext); //模板頁中的showarticle
   str =str.replace("showarticle",strtext); //模板頁中的showarticle str = str.replace("biaoti",strtext);
   str = str.replace("biaoti",strtext); str = str.replace("content",strcontent);
   str = str.replace("content",strcontent); str = str.replace("author",strauthor);
   str = str.replace("author",strauthor); // 寫文件
   // 寫文件 try
   try
 
    {
{ sw = new streamwriter(path + htmlfilename , false, code);
    sw = new streamwriter(path + htmlfilename , false, code); sw.write(str);
    sw.write(str); sw.flush();
    sw.flush(); }
   } catch(exception ex)
   catch(exception ex)
 
    {
{ httpcontext.current.response.write(ex.message);
    httpcontext.current.response.write(ex.message); httpcontext.current.response.end();
    httpcontext.current.response.end(); }
   } finally
   finally
 
    {
{ sw.close();
    sw.close(); }
   } return true;
   return true;
 此函數放在conn.cs基類中了
此函數放在conn.cs基類中了 在添加新聞的代碼中引用 注:工程名為hover
在添加新聞的代碼中引用 注:工程名為hover
 if(hover.conn.writefilethis.title.text.tostring),this.content.text.tostring),this.author.text.tostring)))
    if(hover.conn.writefilethis.title.text.tostring),this.content.text.tostring),this.author.text.tostring)))
 
     {
{ response.write("添加成功");
     response.write("添加成功"); }
    } else
    else
 
     {
{ response.write("生成html出錯!");
     response.write("生成html出錯!"); }
    }  
 
 
 
 模板頁text.html代碼
模板頁text.html代碼

 code:
  code:    <!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html>
<html> <head>
<head> <title>showarticle</title>
  <title>showarticle</title> <body>
   <body> biaoti
biaoti <br>
<br> content<br>
content<br> author
author </body>
</body> </html>
</html> biaoti
biaoti <br>
<br> content<br>
content<br> author
author </body>
</body> </html>
</html> 
新聞熱點
疑難解答
圖片精選