ASP页面请求计数

时间:2013-12-17 15:13:29

标签: asp.net c#-4.0

我有这个ASP页面,并且在每个请求中我想将计数值加1并显示值并且还将计数值保存在文本流中这里是我的代码,但问题是我的txt文件中没有任何内容加载页面

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
    System.IO.StreamWriter sw;
    System.IO.StreamReader sr ;
    int count=0;
    if (!System.IO.File.Exists(@"filepath\ResponsCount.txt"))
    {
        sw = new System.IO.StreamWriter(@"filepath\ResponsCount.txt");
        sw.WriteLine("count.ToString()");
    }

    else
    {
        sr = new System.IO.StreamReader(@"filepath\ResponsCount.txt");
        count = int.Parse(sr.ReadLine());
        count++;
        sw = new System.IO.StreamWriter(@"filepath\ResponsCount.txt");
        sw.WriteLine(count.ToString());
    }
    Label2.Text = count.ToString();
}
</script>

1 个答案:

答案 0 :(得分:0)

我没有看到你关闭了该编写者......

sw.Close();

是您写入文件后应该执行的操作。 然后将所有内容刷新到正在写入的文件,最后正确关闭它。

相关问题