Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
Create Hit Counter using C# - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home ASP.Net Create Hit Counter using C#

Create Hit Counter using C#

E-mail
(1 vote, average: 4.00 out of 5)
A simple Hit Counter function that will count how many visitors your website has reached. This function uses the StreamReader to read from a text file located in the root folder of your application, adn the StreamWriter to write into this file.

First, create a text file, name it as txtcounter.txt, and place it in the root folder or your application.
The GetStat() function will return the latest updated from the text file, and write new stat to it:


protected string  GetStat()
  {
      strFile = Server.MapPath("txtcounter.txt");
      sr = File.OpenText(strFile);
      counter = sr.ReadToEnd();
      sr.Close();
      i = int.Parse(counter) +1;
      counter=i.ToString();
      sw = File.CreateText(strFile);
      sw.Write(counter);
      sw.Close();
      return counter;
}

Call this functin from the Form Load subroutine:
 
protected void Page_Load(object sender, EventArgs e)
   {
            lbl.Text = GetStat();
   }

Happy Coding!

Hit Counter