1. Put the Count.txt and dayCount.txt two files into the App_Data directory
2. Copy the code in the Global.asax.txt to the Global.asax file
3. Where you need to use "Today's visits": application["Daycounter"]
4. Where you need to use "historical visits": application["Counter"]
<%@ application Language= "C #"%><script runat= "Server" >//Features: Website traffic statistics (today visits, total visits)//Ding//Date: 2012-04-12 public static string STRs; Save information read from text public static string day;//record text access time void Application_Start (object sender, EventArgs e) { /********** Total traffic ****************/int intcount = 0; System.IO.StreamReader SR; String serverfile = Server.MapPath ("~/app_data/count.txt"); sr = System.IO.File.OpenText (serverfile); while (Sr. Peek ()! =-1) {string strcount = Sr. ReadLine (); intcount = Int. Parse (Strcount); } Sr. Close (); Object obj = intcount; application["counter"] = obj; /*********************************//********** today visits ****************/application["dayCounter"] = 0; application["Day" = DateTime.Now.ToString (); /*********************************/} void Application_End (objectsender, EventArgs e) {/********** total traffic ****************/int intstat = 0; intstat= (int) application["counter"]; String serverfile = Server.MapPath ("~/app_data/count.txt"); System.IO.StreamWriter sr = new System.IO.StreamWriter (Serverfile, false); Sr. WriteLine (Intstat); Sr. Close (); /*********************************//********** today's visit ****************/int Stat = 0; Stat = (int) application["Daycounter"]; String day0 = (string) application["Day"; Save Date String str = stat.tostring () + "," + day0. ToString (); String file_path = Server.MapPath ("~/app_data/daycount.txt"); Writes a data record to a file System.IO.StreamWriter SRW = new System.IO.StreamWriter (File_path, false); Srw. WriteLine (str); Srw. Close (); /*********************************/} void Application_Error (object sender, EventArgs e) {//appears in Unhandled error when running code}//Statistics total visits void SessioN_start (object sender, EventArgs e) {application.lock (); /********** Total traffic ****************/int intstat = 0; Intstat = (int) application["counter"]; ++intstat; Object objcount = Intstat; application["counter"] = Objcount; String serverfile = Server.MapPath ("~/app_data/count.txt"); System.IO.StreamWriter sr = new System.IO.StreamWriter (Serverfile, false); Sr. WriteLine (Intstat); Sr. Close (); /*********************************//********** today visits ****************/int count;//The daily traffic in the recorded text string Nowday; Record text in Access time System.IO.StreamReader SRD; String file_path = Server.MapPath ("~/app_data/daycount.txt"); Get the actual path to the file SRD = System.IO.File.OpenText (File_path); Open the file for reading while (SRD. Peek ()! =-1) {STRs = SRD. ReadLine (); Save the information read from the file} SRD. Close (); string[] str = STRs. Split (', '); The information to be read is stored in the string array str, COUNT = convert.toInt32 (Str[0]); Daily visits day = str[1]; Last access Time Nowday = DateTime.Now.ToString (); The time value saved in the file is compared to the system time, if the system time is large, restart the count if (Datetime.compare (Convert.todatetime (Nowday), Convert.todatetime (day)) >= 0) {count = 0; Day = DateTime.Now.AddDays (1). ToShortDateString () + "" + "00:00:00"; Day saves the start time of the next one string newdaystr = "0" + "," + days. ToString (); Writes a data record to a file//string File_path0 = Server.MapPath ("Counter.txt"); System.IO.StreamWriter srw0 = new System.IO.StreamWriter (File_path, false); Srw0. WriteLine (NEWDAYSTR); Srw0. Close (); } Object Objcount = Count; Object objday = day; Daily traffic application["daycounter"] = Objcount; Last access time application["Day"] = Objday; Data accumulation int Stat = 0; Gets the daily access amount in the Application object Stat = (int) application["Daycounter"]; Stat + = 1; Object obj = Stat; Application["daycounter"] = obj; Save Date string day0 = (string) application["Day"]; String str0 = obj. ToString () + "," + day0. ToString (); Writes a data record to a file//string File_path0 = Server.MapPath ("Counter.txt"); System.IO.StreamWriter srw1 = new System.IO.StreamWriter (File_path, false); Srw1. WriteLine (STR0); Srw1. Close (); /*********************************/Application.UnLock (); } void Session_End (object sender, EventArgs e) {//session["num"]=convert.toint16 (session["num"]. ToString ())-1; The code that runs at the end of the session. Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to//InProc. If session mode//is set to StateServer or SQL Server, the event is not raised. } </script>
Statistics website Daily visit volume, total visit volume