If you created your own web site, you may have also written simple page hit counters. This project will show you how to create a Windows application to display those hit counters on your desktop.
If you haven't yet created those page hit counters, the code I used for my web site is also included, within this article.
The server code used in this project was created using Web Matrix 2 and the Razor language, for a .NET site.
If your site is not hosted on a .NET server, you'll need to write similar code using your server's language, such as PHP.
The desktop application was created using Visual C# 2010 Express, targeting version 4.0 of the .NET Client Profile. It is a Windows Forms Application.
Using the code
The desktop application sends a
WebRequest to a page on your site. That page creates an XML file stream, containing the page hit data. The desktop application receives that stream, uses it to create an XML document in memory, then parses the XML document, displaying each node in a corresponding
Some of the code from here: http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App was modified, then included in this project.
This is a screenshot of the desktop application:
If you click the title bar of the application, to give it focus, then hover on the Latest data cell, a tool tip will appear, indicating the time of the last stats request.
When you right-click the window, a context menu appears:
Clicking "Preferences" opens the Preferences form.
Here is the table schema of my page hits counter database, in Web Matrix:
You can create a similar database for your site using Web Matrix. This is the code I use in each page to count the daily hits:
The above is for the "Home" page, it adds 1 to the Home cumulative total and 1 to the
hitsToday column. If the latest hit is after "today," then it resets the
hitsToday column to 1.
Once you have created the database and table and inserted the page hit code, create a new .cshtml file named,
Place the following code, modified to suit your circumstances, in
If you don't already have an
App_Code folder in your Web Matrix project, create it, in the root directory, like this:
AccessStats.cshtml file in the App_Code folder, as shown.
Next, create a new .cshtml file named,
CurrentStats.cshtml and place it in the root directory of your Web Matrix site project, as shown.
Place this code in the
Test this code by running the site in Web Matrix and navigating to the
CurrentStats.cshtml file, like this:
You should see an XML stream similar to this, displayed in the browser:
If all is well, publish the new files and database to your site with Web Matrix.
Next, open the Desktop Web Stats project in Visual C# 2010.
The project, on my computer, targeted version 4.0 of the .NET Client Profile.
Right-click the project name, Desktop Web Stats, at the top of the Solution Explorer, then click Properties. You'll see a pane similar to this:
If for any reason the Target Framework is not version 4.0, or you need to target a different version, you must make a change to the
Otherwise, You can run the project, as is, in the debugger to test it against my site.
Next, open the
WebRequestManager.cs file and edit the
statsURL variable to point to the
CurrentStats.cshtml page you created for your web site.
You'll also need to open the
Main.cs page designer and change the text for the header columns and to edit the title bar text.
When you're done, save the project, then right-click the project name, Desktop Web Stats, at the top of the Solution Explorer column. Click Rebuild, to create the .exe file.
Close the project.
Open your Personal Folder and create a new folder named, Desktop Web Stats
Navigate to the bin/Release folder of the Desktop Web Stats Visual C# Project folder. There, you'll find these files:
Desktop Web Stats -- the Application file
Copy those 3 files to the Desktop Web Stats folder you created in your Personal Folder, like this;
Right-click the Application file, then click, Send to, Desktop (create shortcut).
Go to the Desktop and click the shortcut to start the application.
After an initial 8 second delay, your web site stats will be displayed.
Open the Preferences form to edit the settings.
Points of Interest
I prefer to create / delete a .lnk file in the Startup folder instead of inserting / deleting a Registry key. I believe this is a "selling point" for a web developer who wants to offer the client, this desktop web stats application, by saying, "no changes are made to your Registry."
The code makes no provision for any difference between your server time and local time.
Version 1.0 October, 2012