Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Easy Desktop Web Stats

, 23 Oct 2012
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)

About the Author

Michael J Hill - OH

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 23 Oct 2012
Article Copyright 2012 by Michael J Hill - OH
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid