OS.cs :  » GUI » Paint.net » PaintDotNet » SystemLayer » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » Paint.net 
Paint.net » PaintDotNet » SystemLayer » OS.cs
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using Microsoft.Win32;
using System;
using System.Globalization;

namespace PaintDotNet.SystemLayer{
    public static class OS
    {
        /// <summary>
        /// Gets a flag indicating whether we are running on Windows Vista or later.
        /// </summary>
        /// <remarks>
        /// This is the preferred method for detecting this condition, and the most performant
        /// (it avoids creating some temporary Version objects).
        /// </remarks>
        public static bool IsVistaOrLater
        {
            get
            {
                return Environment.OSVersion.Version.Major >= 6;
            }
        }

        public static Version WindowsXP
        {
            get
            {
                return new Version(5, 1);
            }
        }

        public static Version WindowsServer2003
        {
            get
            {
                return new Version(5, 2);
            }
        }

        public static Version WindowsVista
        {
            get
            {
                return new Version(6, 0);
            }
        }

        public static string Revision
        {
            get
            {
                NativeStructs.OSVERSIONINFOEX osviex = new NativeStructs.OSVERSIONINFOEX();
                osviex.dwOSVersionInfoSize = (uint)NativeStructs.OSVERSIONINFOEX.SizeOf;
                bool result = SafeNativeMethods.GetVersionEx(ref osviex);

                if (result)
                {
                    return osviex.szCSDVersion;
                }
                else
                {
                    return "Unknown";
                }
            }
        }

        public static OSType Type
        {
            get
            {
                NativeStructs.OSVERSIONINFOEX osviex = new NativeStructs.OSVERSIONINFOEX();
                osviex.dwOSVersionInfoSize = (uint)NativeStructs.OSVERSIONINFOEX.SizeOf;
                bool result = SafeNativeMethods.GetVersionEx(ref osviex);
                OSType type;

                if (result)
                {
                    if (Enum.IsDefined(typeof(OSType), (OSType)osviex.wProductType))
                    {
                        type = (OSType)osviex.wProductType;
                    }
                    else
                    {
                        type = OSType.Unknown;
                    }
                }
                else
                {
                    type = OSType.Unknown;
                }

                return type;
            }
        }

        public static bool IsDotNetVersionInstalled(int major, int minor, int servicePack, bool allowClientSubset)
        {
            bool result = false;
            
            if (!result)
            {
                // .NET 2.0 should always have a build # of 50727
                result |= IsDotNet2VersionInstalled(major, minor, 50727, servicePack);
            }

            if (!result)
            {
                result |= IsDotNet3VersionInstalled(major, minor, servicePack);
            }

            if (!result && allowClientSubset)
            {
                result |= IsDotNet3ClientVersionInstalled(major, minor, servicePack);
            }

            return result;
        }

        private static bool IsDotNet2VersionInstalled(int major, int minor, int build, int minServicePack)
        {
            bool result = false;

            const string regKeyNameFormat = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}.{2}";
            string regKeyName = string.Format(regKeyNameFormat, major.ToString(CultureInfo.InvariantCulture),
                minor.ToString(CultureInfo.InvariantCulture), build.ToString(CultureInfo.InvariantCulture));

            if (!result)
            {
                const string regValueName = "Install";
                result |= CheckForRegValueEqualsInt(regKeyName, regValueName, 1);
            }

            if (result)
            {
                const string regValueName = "SP";
                int? spLevel = GetRegValueAsInt(regKeyName, regValueName);
                result &= (spLevel.HasValue && spLevel.Value >= minServicePack);
            }

            return result;
        }

        private static bool IsDotNet3VersionInstalled(int major, int minor, int minServicePack)
        {
            bool result = false;

            const string regKeyNameFormat = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}";
            string regKeyName = string.Format(regKeyNameFormat, major, minor);

            if (!result)
            {
                const string regValueName = "Install";
                result |= CheckForRegValueEqualsInt(regKeyName, regValueName, 1);
            }

            if (result)
            {
                const string regValueName = "SP";
                int? spLevel = GetRegValueAsInt(regKeyName, regValueName);
                result &= (spLevel.HasValue && spLevel.Value >= minServicePack);
            }

            return result;
        }

        private static bool IsDotNet3ClientVersionInstalled(int major, int minor, int minServicePack)
        {
            bool result = false;

            const string regKeyNameFormat = "SOFTWARE\\Microsoft\\NET Framework Setup\\DotNetClient\\v{0}.{1}";
            string regKeyName = string.Format(regKeyNameFormat, major, minor);

            if (!result)
            {
                const string regValueName = "Install";
                result |= CheckForRegValueEqualsInt(regKeyName, regValueName, 1);
            }

            if (result)
            {
                const string regValueName = "SP";
                int? spLevel = GetRegValueAsInt(regKeyName, regValueName);
                result &= (spLevel.HasValue && spLevel.Value >= minServicePack);
            }

            return result;
        }

        private static int? GetRegValueAsInt(string regKeyName, string regValueName)
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKeyName, false))
            {
                object value = null;

                if (key != null)
                {
                    value = key.GetValue(regValueName);
                }

                if (value != null && value is int)
                {
                    return (int)value;
                }
                else
                {
                    return null;
                }
            }
        }

        private static bool CheckForRegValueEqualsInt(string regKeyName, string regValueName, int intValue)
        {
            int? value = GetRegValueAsInt(regKeyName, regValueName);

            if (value.HasValue)
            {
                return value.Value == intValue;
            }
            else
            {
                return false;
            }
        }

        public static bool CheckWindowsVersion(int major, int minor, short servicePack)
        {
            NativeStructs.OSVERSIONINFOEX osvi = new NativeStructs.OSVERSIONINFOEX();
            osvi.dwOSVersionInfoSize = (uint)NativeStructs.OSVERSIONINFOEX.SizeOf;
            osvi.dwMajorVersion = (uint)major;
            osvi.dwMinorVersion = (uint)minor;
            osvi.wServicePackMajor = (ushort)servicePack;

            ulong mask = 0;
            mask = NativeMethods.VerSetConditionMask(mask, NativeConstants.VER_MAJORVERSION, NativeConstants.VER_GREATER_EQUAL);
            mask = NativeMethods.VerSetConditionMask(mask, NativeConstants.VER_MINORVERSION, NativeConstants.VER_GREATER_EQUAL);
            mask = NativeMethods.VerSetConditionMask(mask, NativeConstants.VER_SERVICEPACKMAJOR, NativeConstants.VER_GREATER_EQUAL);

            bool result = NativeMethods.VerifyVersionInfo(
                ref osvi,
                NativeConstants.VER_MAJORVERSION |
                    NativeConstants.VER_MINORVERSION |
                    NativeConstants.VER_SERVICEPACKMAJOR,
                mask);

            return result;
        }

        // Requires:
        // * Windows XP SP2 or later
        // * Windows Server 2003 SP1 or later
        // * Windows Vista
        // * or later (must be NT-based)
        public static bool CheckOSRequirement()
        {
            // Just say "no" to Windows 9x
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                return false;
            }

            // Windows Vista or later?
            bool winVista = OS.CheckWindowsVersion(6, 0, 0);

            // Windows 2003 or later?
            bool win2k3 = OS.CheckWindowsVersion(5, 2, 0);

            // Windows 2003 SP1 or later?
            bool win2k3SP1 = OS.CheckWindowsVersion(5, 2, 1);

            // Windows XP or later?
            bool winXP = OS.CheckWindowsVersion(5, 1, 0);

            // Windows XP SP2 or later?
            bool winXPSP2 = OS.CheckWindowsVersion(5, 1, 2);

            return winVista || (win2k3 && win2k3SP1) || (winXP && winXPSP2);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.