Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Easy Desktop Web Stats

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Oct 2012CPOL4 min read 11.6K   120   5   1
Create a Windows desktop app to display daily web stats from your personal site

Introduction 

If you have 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.   

Background  

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 DataGridView box. 

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:  

Image 1
 

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. 

Image 2 

When you right-click the window, a context menu appears: 

Image 3 

Clicking "Preferences" opens the Preferences form. 

Image 4 

Here is the table schema of my page hits counter database, in Web Matrix: 

Image 5

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: 

 Image 6


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, AccessStats.cshtml

Place the following code, modified to suit your circumstances, in AccessStats.cshtml

 Image 7
 

If you don't already have an App_Code folder in your Web Matrix project, create it, in the root directory, like this: 

Image 8 

Place the 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. 

Image 9 

Place this code in the CurrentStats.cshtml file:

Image 10

Test this code by running the site in Web Matrix and navigating to the CurrentStats.cshtml file, like this:

Image 11

 You should see an XML stream similar to this, displayed in the browser:

Image 12 

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:

Image 13

 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 VouchSafe.cs file, here: 

Image 14

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 

Image 15 

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

Interop.IWshRuntimeLibrary.dll

Interop.Shell32.dll

Copy those 3 files to the Desktop Web Stats folder you created in your Personal Folder, like this;

Image 16

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.

History

Version 1.0  October, 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Savalia Manoj M22-Oct-12 20:54
Savalia Manoj M22-Oct-12 20:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.