当前位置: 代码迷 >> ASP.NET >> 关于StreamWriter,StreamReader生成静态页解决思路
  详细解决方案

关于StreamWriter,StreamReader生成静态页解决思路

热度:9975   发布时间:2013-02-25 00:00:00.0
关于StreamWriter,StreamReader生成静态页

protected void Button1_Click(object sender, EventArgs e)
    {
        StreamWriter sw = null;
        StreamReader sr = null;
        string htmlfilename = "";
        string str1 = "";
        string path1 = HttpContext.Current.Server.MapPath("i.htm");
        string path = HttpContext.Current.Server.MapPath("~/");
        try
        {
            sr = new StreamReader(path1, Encoding.GetEncoding("GB2312"));
            str1 = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sr.Close();
        }
        try
        {
            for (int i = 0; i < 4; i++)
            {
                htmlfilename = "ceshi_" + i + "";
                sw = new StreamWriter(path + htmlfilename, false, Encoding.GetEncoding("GB2312"));
                str1 = str1.Replace("$i$", i.ToString());
                sw.Write(str1);
                sw.Flush();
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
    }

模板页,i

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
$i$
</body>
</html>

生成4个页面,问题是每个页面的值都是0,不应该是0,1,2,3么,这是为什么