asp.net统计网站访问次数的代码
首先建立文本文件counter.txt
内容为阿拉伯数字。
.aspx页面内容(主要代码)
<table>
<td>
<div>您是第<%=Application["counter"]%>位访问者</div>
</td>
<aspabel ID="Label1" runat="server" Text="Label"></asp
abel>
</table>
.aspx.cs页面内容
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class foot : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
int counter = 0;
string file_path = Server.MapPath("counter.txt");
StreamReader srd = File.OpenText(file_path);
while (srd.Peek() != -1)
{
string str = srd.ReadLine();
counter = int.Parse(str);
}
srd.Close();
counter = counter + 1;
StreamWriter srw = new StreamWriter(file_path, false);
srw.Write(counter);
srw.Close();
Label1.Text = counter.ToString();
}
}
来源地址:http://www.hlcx.net/thread-14-1-1.html 合力创享