A simple Web-page counter

Source: Internet
Author: User
Tags bool datetime readline
Today wrote a simple counter, the function is simple, can record the total number of visits and the number of visits today. The code is as follows:
Copy Save
// ******************************************************** //Web Counter control//Designed by Faib Studio.//Copyright//Email faib920@126.com or QQ 55570729// ******************************************************** 
usingSystem;usingSystem.ComponentModel;usingSystem.Web.UI;usingSystem.Web.UI.Design;usingSystem.Web.UI.WebControls;usingSystem.Drawing.Design;usingSystem.IO;usingSystem.Text;namespacefaibclass.webcontrols {[ToolBoxData ("<{0}:counter runat=server></{0}:counter>")] Public classCounter:label {#region Private Variables        Private stringM_filename ="";Private stringM_imageurl ="";Private intM_length = 5;Private intM_totalcount, M_todaycount;Private BOOLM_onlyicon =false;#endregion
         PublicCounter () {}#region Properties[Browsable (false)] Public Override stringText {Get{return Base. Text; }Set{Base. Text = value; }} [Category ("Appearance"), Description (the path to the text file where the count is saved. "), Editor (typeof(UrlEditor),typeof(UITypeEditor))] Public stringFileName {Get{returnM_filename; }Set{m_filename = value;} } [Category ("Appearance"), Description ("Use images to display counts, numbers using wildcard characters instead." "), Editor (typeof(Imageurleditor),typeof(UITypeEditor))] Public stringIMAGEURL {Get{returnM_imageurl; }Set{m_imageurl = value;} } [Category ("Appearance"), Description ("Displays the default length of the count. "), DefaultValue (5)] Public intLength {Get{returnM_length; }Set{m_length = value;} } [Category ("Appearance"), Description ("Whether an icon is displayed only on the interface. "), DefaultValue (false)] Public BOOLOnlyicon {Get{returnM_onlyicon; }Set{M_onlyicon = value;} }//Total traffic[Browsable (false)] Public intTotalCount {Get{returnM_totalcount; }Set{m_totalcount = value;} }//Today visits[Browsable (false)] Public intTodaycount {Get{returnM_todaycount; }Set{m_todaycount = value;} }#endregion#region Public Methods        protected Override voidOnLoad (EventArgs e) {intCounter = 0, Todaycounter = 0;
            DateTime today = Datetime.minvalue; StringBuilder Builder =NewStringBuilder ();if(Util.isdesignmode (Page))//Whether design mode{Random rad =NewRandom ();if(M_imageurl.length = = 0) {if(M_onlyicon) {Builder. Append ("  " "+ M_imageurl +"/" align=absmiddle> "); }Else{ for(inti = 0; i < m_length; i++) {Builder. Append (RAD. Next (9).
                        ToString ()); }
                    }
                }Else{if(M_onlyicon) {Builder. Append ("  " "+ M_imageurl +"/" align=absmiddle> "); }Else{ for(inti = 0; i < m_length; i++) {Builder. Append ("  " "+ M_imageurl.replace ("?"Rad. Next (9). ToString ()) +"/" align=absmiddle> "); } } } }Else{BOOLBlnexists = File.exists (Page.Server.MapPath (m_filename));if(blnexists)//read out the number of visits{StreamReader str = file.opentext (Page.Server.MapPath (m_filename));Try{counter =int. Parse (str. ReadLine ()); Todaycounter =int. Parse (str. ReadLine ()); Today = DateTime.Parse (str. ReadLine ()); }Catch{Throw NewException ("The file content is empty, please delete the file"); }finally{str. Close (); } }if(page.session["__counter"] ==NULL) {counter++; todaycounter++;if(Today! = Datetime.today) {today = Datetime.today; } }strings = counter. ToString (); Attributes.Add ("title","Total:"+ S +", Today:"+ Todaycounter. ToString ());if(M_onlyicon) {Builder. Append ("  " "+ M_imageurl +"/" align=absmiddle> "); }Else{if(S.length < M_length) {if(M_imageurl.length = = 0) { for(inti = 0; i < m_length-s.length; i++) {Builder. Append ("0"); } }Else{ for(inti = 0; i < m_length-s.length; i++) {Builder. Append ("  " "+ M_imageurl.replace ("?","0") +"/" align=absmiddle> "); } } }if(M_imageurl.length = = 0) {Builder. Append (s); }Else{ for(inti = 0; i < s.length; i++) {Builder. Append ("  " "+ M_imageurl.replace ("?", S.substring (i, 1)) +"/" align=absmiddle> "); } } }//write new traffic to the file if(page.session["__counter"] ==NULL) {StreamWriter STW =NewStreamWriter (Page.Server.MapPath (m_filename)); StW. WriteLine (counter. ToString ());//write totalsStW. WriteLine (Todaycounter. ToString ());//write DateStW. WriteLine (today. ToShortDateString ());//write today TrafficStW. Close (); page.session["__counter"] = 1;//Logo}} Text = Builder. ToString (); M_totalcount = counter; M_todaycount = Todaycounter;Base. OnLoad (e); }#endregion} }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.