小编Hap*_*NET的帖子

如何在asp.net c#中计算网站访问者数量

如何计算asp.net c#中网站的访问者数量?

我使用下面的代码:

在global.asax页面中

<%@ Application Language="C#" %>
Run Code Online (Sandbox Code Playgroud)

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Application["NoOfVisitors"] = 0;
}





void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
    Application.UnLock();
}
Run Code Online (Sandbox Code Playgroud)

在.aspx页面

<asp:Label runat="server" ID="lbluser" />
Run Code Online (Sandbox Code Playgroud)

在.aspx.cs中

protected void Page_Load(object sender, EventArgs e)
{
    lbluser.Text = Application["NoOfVisitors"].ToString();
}
Run Code Online (Sandbox Code Playgroud)

应用程序计数器每小时重置为0 ...在计算用户数量方面我在哪里错了?...感谢你...

c# asp.net global

6
推荐指数
1
解决办法
2万
查看次数

如何创建.txt文件并将其写入c#asp.net

我正在尝试使用以下代码创建并写入C#应用程序中的文本文件

System.IO.Directory.CreateDirectory(Server.MapPath("~\\count"));

using (System.IO.FileStream fs = new System.IO.FileStream("~/count/count.txt", System.IO.FileMode.Create))
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("~/count/count.txt"))
{
    sw.Write("101");
}

string _count = System.IO.File.ReadAllText("~/count/count.txt");
Application["NoOfVisitors"] = _count;
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:

该进程无法访问文件"路径",因为它正由另一个进程使用.

我的错误是什么?

c# asp.net text-files

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net ×2

c# ×2

global ×1

text-files ×1