Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C# 4.0

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  
Create a Windows desktop app to display daily web stats from your personal site
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Win32;

using MessageManager;

namespace Compatibility
{
    class VouchSafe
    {
        public delegate bool VouchSafeDelegate();
        public VouchSafeDelegate compatibility;

        private short compatibilityErrors;

        public VouchSafe()
        {
            // append methods to delegate;
            compatibility += Test_ForAlreadyRunning;
            compatibility += Test_ForDotNetVersion;
            compatibility += Test_ForWindowsProductName;
            compatibility += CompatibilityStatus;

            compatibilityErrors = 0;
        }
        
        private bool Test_ForAlreadyRunning()
        {
            DialogResult result;
            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                result = Dialog.Message("Error", 
                                        "The program is already running.",
                                         MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (result == DialogResult.OK)
                {
                    compatibilityErrors++;                 
                }
            }
            return true;
        }

        private bool Test_ForDotNetVersion()
        {
            string framework;
            string versionStr = "";
            float version = 0;
            RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Active Setup\\Installed Components");
            string[] subKeys = componentsKey.GetSubKeyNames();
            foreach (string subKey in subKeys)
            {
                RegistryKey key = componentsKey.OpenSubKey(subKey);
                framework = (string)key.GetValue(null); 
                if (framework != null  && framework.IndexOf(".NET Framework") != -1)
                {
                    versionStr = (string)key.GetValue("Version");
                }
            }
            versionStr = versionStr.Replace(",", ".");
            Regex rx = new Regex(@"^(\d+)(.+)$");
            versionStr = rx.Replace(versionStr, "$1");
            version = float.Parse(versionStr);       
   
            // this project targets version 4.0 of the .NET Client Profile
            // if necessary, modify the next block, to suit your project

            if (version < 4)
            {
                DialogResult result = Dialog.Message("Error",
                                        "Version 4.0 or better of the Microsoft .NET\nFramework Client Profile must be installed\nto use this program.",
                                         MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (result == DialogResult.OK)
                {
                    compatibilityErrors++;                                     
                }
            }
            return true;
        }

        private bool Test_ForWindowsProductName()
        {
            DialogResult result;
            RegistryKey hkey = Registry.LocalMachine;
            RegistryKey productNameKey = hkey.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
            string productName = (string)productNameKey.GetValue("ProductName").ToString();
            if (productName.IndexOf("Windows 8") != -1)
            {
                return true;
            }
            if (productName.IndexOf("Windows 7") != -1)
            {
                return true;
            }
            if (productName.IndexOf("Vista") != -1)
            {
                return true;
            }

            result = Dialog.Message("Error", 
                                    "You need Windows 7, 8 or Vista\nto use this program.",
                                     MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (result == DialogResult.OK)
            {
                compatibilityErrors++;                 
            }
            return true;
        }

        private bool CompatibilityStatus()
        {
            if (compatibilityErrors != 0)
            {
                return false;
            }
            return true;
        }
    }    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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