Try these steps as
step 1: Open A project and add a text file in it
Step 2:Then create the page in which you need to place the hit counter
step 3:In page load event call the coding as
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
Application.Lock();
Response.Write(gethitcounts()); //you can change here for label control
Application.UnLock();
}
public string gethitcounts()
{
string lastcount = "";
try
{
StreamReader SR = File.OpenText(Server.MapPath("hitcounter.txt"));
string getcount = null;
while ((getcount = SR.ReadLine()) != null)
{
lastcount = lastcount + getcount;
}
SR.Close();
long newcount = Convert.ToInt64(lastcount);
newcount++;
TextWriter TxtWtr = new StreamWriter(Server.MapPath("hitcounter.txt"));
TxtWtr.WriteLine(Convert.ToString(newcount));
TxtWtr.Close();
SR = File.OpenText(Server.MapPath("hitcounter.txt"));
getcount = null;
lastcount = "";
while ((getcount = SR.ReadLine()) != null)
{
lastcount = lastcount + getcount;
}
SR.Close();
}
catch (Exception ex)
{
TextWriter TxtWtr = new StreamWriter(Server.MapPath("hitcounter.txt"));
TxtWtr.WriteLine(Convert.ToString("1"));
TxtWtr.Close();
lastcount = "1";
}
return lastcount;
}
Thats it