用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - asp代码库

用模板生成html页面

2012-10-22 作者: 程序猿style举报

[asp]代码库

//使用模板生成HTML页
public static bool WriteFile ( string strText, string strContent, string strAuthor )
{
    string path = HttpContext.Current.Server.MapPath ( "~/news/" );
    Encoding code = Encoding.GetEncoding ( "gb2312" );
    // 读取模板文件
    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(); // 读取文件
    }
    catch ( Exception exp )
    {
        HttpContext.Current.Response.Write ( exp.Message );
        HttpContext.Current.Response.End();
        sr.Close();
    }
 
 
    string htmlfilename = DateTime.Now.ToString ( "yyyyMMddHHmmss" ) + ".html";
    // 替换内容
    // 这时,模板文件已经读入到名称为str的变量中了
    str = str.Replace ( "ShowArticle", strText ); //模板页中的ShowArticle
    str = str.Replace ( "biaoti", strText );
    str = str.Replace ( "content", strContent );
    str = str.Replace ( "author", strAuthor );
    // 写文件
    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;
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...