如题
我现在用网上常见的一个方法:如下
string path = HttpContext.Current.Server.MapPath("../html/");
        Encoding code = Encoding.GetEncoding("gb2312");
        // 读取模板文件   
        string temp = HttpContext.Current.Server.MapPath("../html/Temp.htm");
        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 =path+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
        // 替换内容   
        // 这时,模板文件已经读入到名称为str的变量中了   
        str = str.Replace("ShowArticle", strText); //模板页中的ShowArticle   
        str = str.Replace("title", strText);
        str = str.Replace("content", strContent);
        str = str.Replace("author", strAuthor);
        // 写文件   
        try
        {
            sw = new StreamWriter(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;
不知道是什么原因,执行操作提示成功,就是没有看到生成后的htm文件
这个方法好像也不能创建文件夹,我在网上看到很多人说用FSO 但是我不懂什么意思 到底要怎么弄才可以实现 自动创建如:20110822/XXXX.htm这种样子的
请各位哥哥姐姐帮忙解决下,

解决方案 »

  1.   

    创建文件夹
    if(!Directory.Exists(Path)) 

    Directory.CreateDirectory(Path); 

      

  2.   

    例码:
    protected bool makehtmlpage()
         {
             StreamWriter sw = null;
             try
             {
                 string sFileName = Server.MapPath("~/html/2011/08/22/");
                 if (!Directory.Exists(sFileName))
                 {
                     Directory.CreateDirectory(sFileName);
                 }             sw = new StreamWriter(sFileName + "1A2C5DF89DR48EH5.html", false, System.Text.Encoding.UTF8);             StringWriter stringW = new StringWriter();
                 HttpContext.Current.Server.Execute("~/show.aspx?ID=152753682", stringW);
                 
                 sw.Write(stringW.ToString());
                 sw.Close();
                 return true;
             }         catch (Exception ex)
             {
                 //这里必须关闭,否则内存占用增长....
                 if (sw != null) sw.Close();
                 
                 return false;
             }
                finally
                {
                    //这里必须关闭,否则内存占用增长....
                    if(sw!=null)sw.Close();
                }
         }
      

  3.   

    谢谢各位哥哥姐姐,问题解决了,我一开始忘了刷新文件夹。
    不过还是感谢各位,现在Head部分有点问题 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <LS下周六比赛活动>LS下周六比赛活动</LS下周六比赛活动>
        
    <style type="text/css">
    <!--
    body {
     background-color: #FFFFFF;
     margin-left: 0px;
     margin-top: 0px;
     margin-right: 0px;
     margin-bottom: 0px;
     font-size:12px;
    }
    .tabtext {color: #FFFFFF}
    -->
    </style>
    </head>
    <body>
    标题标签那里 直接显示的文本内容 怎么样才能变成<title></title>呢
    这个关键字和描述怎么添加啊 我用HtmlMeat这个类试过 不行
      

  4.   

    str = str.Replace("ShowArticle", strText); //模板页中的ShowArticle   
            str = str.Replace("titles", strText); //把原来这里的title改为titles就可以了
            str = str.Replace("content", strContent);
            str = str.Replace("author", strAuthor);
    关键字和描述也是同理,替换模版类容是替换所有的,包括<title></title>标签和<meta name="keywords" content="cttkeywords"/>标签里面的内容,所以关键字和描述你可以把cttkeywords替换成你想要的内容就可以了