我给网站添加了Global.asax,文本文件Site_Counter.txt并写入数字0,代码分别如下:
Global.asax
<%
@ Application Language
=
"
C#
"
%>
< script runat = " server " >
void Application_Start( object sender, EventArgs e)
... {
// 在应用程序启动时运行的代码
//读取网站访问数
System.IO.StreamReader rd = new System.IO.StreamReader(Server.MapPath("Site_Counter.txt"));
Application.Lock();
Application["Site_Counter"] = long.Parse(rd.ReadLine());
Application.UnLock();
rd.Close();
}
void Session_Start( object sender, EventArgs e)
... {
// 在新会话启动时运行的代码
//更新网站访问数
Application.Lock();
Application["Site_Counter"] = Convert.ToInt64(Application["Site_Counter"]) + 1;
Application.UnLock();
System.IO.StreamWriter wr = new System.IO.StreamWriter(Server.MapPath("Site_Counter.txt"), false);
Application.Lock();
wr.WriteLine(Application["Site_Counter"]);
Application.UnLock();
wr.Close();
}
</ script >
< script runat = " server " >
void Application_Start( object sender, EventArgs e)
... {
// 在应用程序启动时运行的代码
//读取网站访问数
System.IO.StreamReader rd = new System.IO.StreamReader(Server.MapPath("Site_Counter.txt"));
Application.Lock();
Application["Site_Counter"] = long.Parse(rd.ReadLine());
Application.UnLock();
rd.Close();
}
void Session_Start( object sender, EventArgs e)
... {
// 在新会话启动时运行的代码
//更新网站访问数
Application.Lock();
Application["Site_Counter"] = Convert.ToInt64(Application["Site_Counter"]) + 1;
Application.UnLock();
System.IO.StreamWriter wr = new System.IO.StreamWriter(Server.MapPath("Site_Counter.txt"), false);
Application.Lock();
wr.WriteLine(Application["Site_Counter"]);
Application.UnLock();
wr.Close();
}
</ script >
然后在需要显示的页面显示系统 就可以了
比如简单的:
Label_Site_Counter.Text
=
Convert.ToString(Application[
"
Site_Counter
"
]);
假如想显示为图片的话,可以请美工做0-9个数字图片,然后加入下面的代码:呵呵,大功告成,测试下吧,是不是已经看到了呢?
counter
=
(
long
)Application[
"
Site_Counter
"
];
public string piclong( long counter)
... {
string returns="";
for(int i=1;i<=counter.ToString().Length;i++)
...{
returns+="<td><img src='Image/"+counter.ToString().Substring(i-1, 1)+".gif' width='14' height='19'></td>";
}
return returns;
}
public string piclong( long counter)
... {
string returns="";
for(int i=1;i<=counter.ToString().Length;i++)
...{
returns+="<td><img src='Image/"+counter.ToString().Substring(i-1, 1)+".gif' width='14' height='19'></td>";
}
return returns;
}
参考:http://hi.baidu.com/hehenb/blog/item/5a734766ed35a024ab184cd6.html