0
In some sites you may have seen visitors counter/Total hits at the bottom of the website, in asp.net it is very easy to implement this feature, here i will show you a simple example that stores visitors count in a Text file.
First you have to add a Global.asax file into the solution you are working, you can do this by Right clicking your project name in the solution explorer ->Add New Item -> Global Application Class.
create a Hits.txt file in the root folder of your application and just add following code in the Session_Start event in the Global.asax file.
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/Hits.txt")))
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
string count = sr.ReadToEnd();
sr.Dispose();
Application["Hits"] = int.Parse(count) + 1;
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
sw.Write(Application["Hits"].ToString());
sw.Dispose();
}
//Application["Hits"] = int.Parse(Application["Hits"].ToString()) + 1;
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
Reading Data from Hits.text file:
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/TestFile.txt"))){
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
string strdata = sr.ReadToEnd();
sr.Dispose();
}
Here total hits will be available in "strdata" variable.
we have to create a StreamReader object to read data from a text file, follow this MSDN article to know more about StreamReader class.
First you have to add a Global.asax file into the solution you are working, you can do this by Right clicking your project name in the solution explorer ->Add New Item -> Global Application Class.
create a Hits.txt file in the root folder of your application and just add following code in the Session_Start event in the Global.asax file.
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/Hits.txt")))
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
string count = sr.ReadToEnd();
sr.Dispose();
Application["Hits"] = int.Parse(count) + 1;
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
sw.Write(Application["Hits"].ToString());
sw.Dispose();
}
//Application["Hits"] = int.Parse(Application["Hits"].ToString()) + 1;
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
Reading Data from Hits.text file:
if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath + "/TestFile.txt"))){
{
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath(Request.ApplicationPath + "/Hits.txt"));
string strdata = sr.ReadToEnd();
sr.Dispose();
}
Here total hits will be available in "strdata" variable.
we have to create a StreamReader object to read data from a text file, follow this MSDN article to know more about StreamReader class.
Post a Comment