Simple BlogEngine.NET Download Counter Extension

SDCounter A few months back, I made a file download counter for my blog.  I've been planning to make it available forever and tonight I was asked for a copy of the code so I figured I might as well make it available to everyone.  It is not too exciting and could likely use some code cleanup, but here is what has been running on my blog and on the main BlogEngine.NET site for a while.

The download counter is very simple.  It is all stored in a single cs file that you add to your App_Code\Extensions folder.  Once you place the file there, you are done and it will start recording file downloads that go through the BlogEngine.NET file handler. 

It is important to note that I wrote it counts files downloaded through file handler and not just any file you link to.  If you upload files to BlogEngine.NET through the Admin panel, they are stored in the App_Data\files folder.  (Yes, you can add folders like Windows Live Writer does to organize things a bit, but I'm going to keep this explanation simple.)  When files are upload there, you can access them through the file handler with a link in this format:

http://mysite.com/blog/file.axd?file=filename.ext

For those unaware, if you upload a file while writing a post, it will make the correct link for you and you really don't need to worry about any of this.

Ok, now that we have that explained, let me tell you a bit more about how this works.  Each time a file is served, an xml file is parsed.  (This xml will be created the first time a file is downloaded and be stored in your App_Data folder.  This will change in BlogEngine.NET 1.4 when this is provider based.)  It looks through it to see if the file downloaded is already there and if it is, it increments the download count.  If it is not in the file, it just adds it to the file with a count of 1.

SDCounterCloseupThe site visitor will not see the count anywhere on the site, but once you login to your blog, you will see the download links with a file counter on them.  Here is a close up of the screen shot above.  This is what I saw when I logged in tonight.  If you go and check out the NonZero Theme post, you'll notice the link doesn't include the download count.

This is just one download counter option for you.  Ruslan Tur has another download counter option available for you to check out as well.  I renamed my extension SimpleDownloadCounter so as not to conflict with Ruslan's fine counter.

Download: SimpleDownloadCounter.zip