通過使用者對網站的訪問,來統計該網站的總訪問量.實現起來非常簡單.把每一次訪問的數累加即可.由於資料
更新頻繁,所以資料存放區在文字檔中.每次更新其中的文字檔資料即可.
然後把數字替換成相應圖片.不知道為何,CSDN又不能上傳圖片了.CSDN的blog做得很不穩定..圖略.(非常簡單)
代碼如下;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace Counter
... {
/**//// <summary>
/// Test 的摘要說明。
/// </summary>
public class Test : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.Label Count;
long counter=1;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此處放置使用者代碼以初始化頁面
if (!IsPostBack)
...{
Application.Lock();//進行鎖定,防止非同步更新
string countfile=Server.MapPath("count.txt");
if(File.Exists(countfile))
...{
StreamReader sReader=new StreamReader(countfile);
string s;
long x;
s=sReader.ReadLine();
if(s!=null )
...{
x=Convert.ToInt64(s);
counter=x+1;
}
sReader.Close();
}
StreamWriter sWriter=new StreamWriter(countfile);
sWriter.Write((counter.ToString()));//將long類型轉換為string類型
sWriter.Flush();
sWriter.Close();
Application.UnLock();
Count.Text=piclong(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;
}
Web 表單設計器產生的程式碼#region Web 表單設計器產生的程式碼
override protected void OnInit(EventArgs e)