// // Copyright © 2010 All Rights Reserved. Test developers can simply copy and // paste the code into their code, but may not reproduce or publish the code // snippets on any web site, online service, or distribute as source on any // media without express written permission. namespace TestingMentor.Snippet.OperatingSystemVersionInfo { using System; using System.Runtime.InteropServices; /// /// This class gets Windows operating system version information to better /// control flow of automated test case (uint)s based on platform profiles, or /// to provide information about the operating system a distributed test /// is executed on for troubleshooting purposes. /// public class WindowsVersionInfo { #region PUBLIC PROPERTIES /// /// Gets the operating system version information /// public string GetOSVersion { get { return this.GetOSVersionInfo(); } } /// /// Gets the Service Pack information /// public string GetServicePack { get { return this.GetServicePackInfo(); } } /// /// Gets the Product Type information /// public string GetProductType { get { return this.GetProductTypeInfo(); } } #endregion PUBLIC PROPERTIES #region PRIVATE METHODS /// /// This method gets the version of currently supported Windows operating /// systems based on the major and minor version numbers and other /// information in OSVERSIONINFOEX struct /// /// A string indicating the Windows OS; otherwise a string /// indicating the OS is unsupported. private string GetOSVersionInfo() { string version = "Unsupported Version"; NativeMethods.OSVersionInfoEx osvi = new NativeMethods.OSVersionInfoEx(); osvi.VersionInfoSize = Marshal.SizeOf(typeof(NativeMethods.OSVersionInfoEx)); NativeMethods.GetVersionEx(ref osvi); if (OsviConstant.SupportedPlatform == osvi.PlatformId && osvi.MajorVersion > 4) { if (osvi.MajorVersion == (int)OsviConstant.MajorVersion.NT5 && osvi.MinorVersion == (int)OsviConstant.MinorVersion.Windows2000) { version = "Windows 2000"; } if (osvi.MajorVersion == (int)OsviConstant.MajorVersion.NT5 && osvi.MinorVersion == (int)OsviConstant.MinorVersion.WindowsXP) { version = "Windows XP"; } if (osvi.MajorVersion == (int)OsviConstant.MajorVersion.NT5 && osvi.MinorVersion == (int)OsviConstant.MinorVersion.WindowsServer2003) { if (osvi.ProductType == (byte)OsviConstant.WorkStation) { version = "Windows XP Professional x64"; } else { version = "Windows Server 2003"; if (NativeMethods.GetSystemMetrics(OsviConstant.ServerR2) != 0) { version += " R2"; } } } if (osvi.MajorVersion == (int)OsviConstant.MajorVersion.NT6 && osvi.MinorVersion == (int)OsviConstant.MinorVersion.WindowsVista) { if (osvi.ProductType == (byte)OsviConstant.WorkStation) { version = "Windows Vista"; } else { version = "Windows Server 2008"; } } if (osvi.MajorVersion == (int)OsviConstant.MajorVersion.NT6 && osvi.MinorVersion == (int)OsviConstant.MinorVersion.Windows7) { if (osvi.ProductType == (byte)OsviConstant.WorkStation) { version = "Windows 7"; } else { version = "Windows Server 2008 R2"; } } } return version; } /// /// This method gets the service pack information for the current operating /// system. /// /// A string indicating the Service Pack installed; otherwise an /// empty string private string GetServicePackInfo() { NativeMethods.OSVersionInfoEx versionInfo = new NativeMethods.OSVersionInfoEx(); versionInfo.VersionInfoSize = Marshal.SizeOf(typeof(NativeMethods.OSVersionInfoEx)); NativeMethods.GetVersionEx(ref versionInfo); return versionInfo.CSDVersion; } /// /// This method gets the product type information for Windows Vista and /// newer releases of the Windows family of operating systems /// /// A string indicating the OS product type; otherwise an empty /// string. private string GetProductTypeInfo() { string product = String.Empty; NativeMethods.OSVersionInfoEx osvi = new NativeMethods.OSVersionInfoEx(); osvi.VersionInfoSize = Marshal.SizeOf(typeof(NativeMethods.OSVersionInfoEx)); NativeMethods.GetVersionEx(ref osvi); if (osvi.MajorVersion > 5) { uint productType = 0; NativeMethods.GetProductInfo( osvi.MajorVersion, osvi.MinorVersion, osvi.ServicePackMajor, osvi.ServicePackMinor, ref productType); switch (productType) { case (uint)OsviConstant.ProductInfo.Business: product = "Business Edition"; break; case (uint)OsviConstant.ProductInfo.BusinessN: product = "Business N Edition"; break; case (uint)OsviConstant.ProductInfo.ClusterServer: product = "HPC Edition"; break; case (uint)OsviConstant.ProductInfo.DatacenterServer: product = "Server Datacenter (Full)"; break; case (uint)OsviConstant.ProductInfo.DatacenterServerCore: product = "Server Datacenter (Core)"; break; case (uint)OsviConstant.ProductInfo.DataCenterServerCoreV: product = "Server Datacenter without Hyper-V (Core)"; break; case (uint)OsviConstant.ProductInfo.DataCenterServerV: product = "Server Datacenter without Hyper-V (Full)"; break; case (uint)OsviConstant.ProductInfo.Enterprise: product = "Enterprise Edition"; break; case (uint)OsviConstant.ProductInfo.EnterpriseE: product = "Enterprise E Edition"; break; case (uint)OsviConstant.ProductInfo.EnterpriseN: product = "Enterprise N Edition"; break; case (uint)OsviConstant.ProductInfo.EnterpriseServer: product = "Server Enterprise (Full)"; break; case (uint)OsviConstant.ProductInfo.EnterpriseServerCore: product = "Server Enterprise (Core)"; break; case (uint)OsviConstant.ProductInfo.EnterpriseServerCoreV: product = "Server Enterprise without Hyper-V (Core)"; break; case (uint)OsviConstant.ProductInfo.EnterpriseServerIA64: product = "Server Enterprise for Itanium-based Systems"; break; case (uint)OsviConstant.ProductInfo.EnterpriseServerV: product = "Server Enterprise without Hyper-V (Full)"; break; case (uint)OsviConstant.ProductInfo.HomeBasic: product = "Home Basic Edition"; break; case (uint)OsviConstant.ProductInfo.HomeBasicE: product = "Home Basic E Edition"; break; case (uint)OsviConstant.ProductInfo.HomeBasicN: product = "Home Basic N Edition"; break; case (uint)OsviConstant.ProductInfo.HomePremium: product = "Home Premium Edition"; break; case (uint)OsviConstant.ProductInfo.HomePremiumE: product = "Home Premium E Edition"; break; case (uint)OsviConstant.ProductInfo.HomePremiumN: product = "Home Premium N Edition"; break; case (uint)OsviConstant.ProductInfo.HomeServer: product = "Home Server Edition"; break; case (uint)OsviConstant.ProductInfo.HyperV: product = "Microsoft Hyper-V Server"; break; case (uint)OsviConstant.ProductInfo.MediumBusinessServerManagement: product = "Windows Essential Business Server Management Server"; break; case (uint)OsviConstant.ProductInfo.MediumBusinessServerMessaging: product = "Windows Essential Business Server Messaging Server"; break; case (uint)OsviConstant.ProductInfo.MediumBusinessServerSecurity: product = "Windows Essential Business Server Security Server"; break; case (uint)OsviConstant.ProductInfo.Professional: product = "Professional Edition"; break; case (uint)OsviConstant.ProductInfo.ProfessionalE: product = "Professional E Edition"; break; case (uint)OsviConstant.ProductInfo.ProfessionalN: product = "Professional N Edition"; break; case (uint)OsviConstant.ProductInfo.ServerForSmallBusiness: product = "Windows Server 2008 for Windows Essential Server Solutions"; break; case (uint)OsviConstant.ProductInfo.ServerForSmallBusinessV: product = "Windows Server 2008 without Hyper-V for Windows Essential Server Solutions"; break; case (uint)OsviConstant.ProductInfo.ServerFoundation: product = "Server Foundation"; break; case (uint)OsviConstant.ProductInfo.SmallBusinessServer: product = "Windows Small Business Server"; break; case (uint)OsviConstant.ProductInfo.SmallBusinessServerPremium: product = "Windows Small Busines Server Premium"; break; case (uint)OsviConstant.ProductInfo.StandardServer: product = "Server Standard (Full)"; break; case (uint)OsviConstant.ProductInfo.StandardServerCore: product = "Server Standard (Core)"; break; case (uint)OsviConstant.ProductInfo.StandardServerCoreV: product = "Server Standard without Hyper-V (Core)"; break; case (uint)OsviConstant.ProductInfo.StandardServerV: product = "Server Standard without Hyper-V (Full)"; break; case (uint)OsviConstant.ProductInfo.Starter: product = "Starter Edition"; break; case (uint)OsviConstant.ProductInfo.StarterE: product = "Starter E Edition"; break; case (uint)OsviConstant.ProductInfo.StarterN: product = "Starter N Edition"; break; case (uint)OsviConstant.ProductInfo.StorageEnterpriseServer: product = "Storage Server Enterprise"; break; case (uint)OsviConstant.ProductInfo.StorageExpressServer: product = "Storage Server Express"; break; case (uint)OsviConstant.ProductInfo.StorageStandardServer: product = "Storage Server Standard"; break; case (uint)OsviConstant.ProductInfo.StorageWorkgroupServer: product = "Storage Server Workgroup"; break; case (uint)OsviConstant.ProductInfo.Ultimate: product = "Ultimate Edition"; break; case (uint)OsviConstant.ProductInfo.UltimateE: product = "Ultimate E Edition"; break; case (uint)OsviConstant.ProductInfo.UltimateN: product = "Ulitmate N Edition"; break; case (uint)OsviConstant.ProductInfo.Undefined: product = "Unknown Product"; break; case (uint)OsviConstant.ProductInfo.Unlicensed: product = "Unlicensed or Expired"; break; case (uint)OsviConstant.ProductInfo.WebServer: product = "Web Server (Full)"; break; case (uint)OsviConstant.ProductInfo.WebServerCore: product = "Web Server (Core)"; break; } } return product; } #endregion PRIVATE METHODS } // **************************************************************************** // NEW CLASS - SHOULD BE PLACED IN SEPARATE FILE // **************************************************************************** /// /// This class contains constant values for version information /// internal class OsviConstant { /// /// Constant value for supported platforms as indicated by PlatformID /// internal const int SupportedPlatform = 2; /// /// Constant value indicating R2 release of Windows Server used by /// GetSystemMetrics function /// internal const int ServerR2 = 89; /// /// Constant value indicating the OS ProductType is a workstation /// internal const int WorkStation = 0x00000001; /// /// Prevents a default instance of the OsviConstant class from being /// created. /// private OsviConstant() { } /// /// Constant values for Windows kernel major versions /// internal enum MajorVersion { /// /// Major kernel versions pre-Vista /// NT5 = 5, /// /// Major kernel versions Vista and higher /// NT6 = 6 } /// /// Constant values for Windows kernel minor versions /// internal enum MinorVersion { /// /// Minor kernel versions for Windows 2000 /// Windows2000 = 0, /// /// Minor kernel versions for Window XP /// WindowsXP = 1, /// /// Minor kernel versions for Windows Server 2003 /// WindowsServer2003 = 2, /// /// Minor kernel versions for Windows Vista /// WindowsVista = 0, /// /// Minor kernel versions for Windows 7 /// Windows7 = 1 } /// /// Constant values for Windows product type /// internal enum ProductInfo : uint { /// /// Business /// Business = 0x00000006, /// /// Business N /// BusinessN = 0x00000010, /// /// HPC Edition /// ClusterServer = 0x00000012, /// /// Server Datacenter (full installation) /// DatacenterServer = 0x00000008, /// /// Server Datacenter (core installation) /// DatacenterServerCore = 0x0000000C, /// /// Server Datacenter without Hyper-V (core installation) /// DataCenterServerCoreV = 0x00000027, /// /// Server Datacenter without Hyper-V (full installation) /// DataCenterServerV = 0x00000025, /// /// Enterprise /// Enterprise = 0x00000004, /// /// Enterprise E /// EnterpriseE = 0x00000046, /// /// Enterprise N /// EnterpriseN = 0x0000001B, /// /// Server Enterprise (full installation) /// EnterpriseServer = 0x0000000A, /// /// Server Enterprise (core installation) /// EnterpriseServerCore = 0x0000000E, /// /// Server Enterprise without Hyper-V (core installation) /// EnterpriseServerCoreV = 0x00000029, /// /// Server Enterprise for Itanium-based Systems /// EnterpriseServerIA64 = 0x0000000F, /// /// Server Enterprise without Hyper-V (full installation) /// EnterpriseServerV = 0x00000026, /// /// Home Basic /// HomeBasic = 0x00000002, /// /// Home Basic E /// HomeBasicE = 0x00000043, /// /// Home Basic N /// HomeBasicN = 0x00000005, /// /// Home Premium /// HomePremium = 0x00000003, /// /// Home Premium E /// /// HomePremiumE = 0x00000044, /// /// Home Premium N /// HomePremiumN = 0x0000001A, /// /// Microsoft Hyper-V Server /// HyperV = 0x0000002A, /// /// Windows Essential Business Server Management Server /// MediumBusinessServerManagement = 0x0000001E, /// /// Windows Essential Business Server Security Server /// MediumBusinessServerSecurity = 0x0000001F, /// /// Windows Essential Business Server Messaging Server /// MediumBusinessServerMessaging = 0x00000020, /// /// Professional /// Professional = 0x00000030, /// /// Professional E /// ProfessionalE = 0x00000045, /// /// Professional N /// ProfessionalN = 0x00000031, /// /// Windows Server 2008 for Windows Essential Server Solutions /// ServerForSmallBusiness = 0x00000018, /// /// Windows Server 2008 without Hyper-V for Windows Essential Server /// Solutions /// ServerForSmallBusinessV = 0x00000023, /// /// Server Foundation /// ServerFoundation = 0x00000021, /// /// Windows Small Business Server /// SmallBusinessServer = 0x00000009, /// /// Server Standard (full installation) /// StandardServer = 0x00000007, /// /// Server Standard (core installation) /// StandardServerCore = 0x0000000D, /// /// Server Standard without Hyper-V (core installation) /// StandardServerCoreV = 0x00000028, /// /// Server Standard without Hyper-V (full installation) /// StandardServerV = 0x00000024, /// /// Starter /// Starter = 0x0000000B, /// /// Starter E /// StarterE = 0x00000042, /// /// Starter N /// StarterN = 0x0000002F, /// /// Storage Server Enterprise /// StorageEnterpriseServer = 0x00000017, /// /// Storage Server Express /// StorageExpressServer = 0x00000014, /// /// Storage Server Standard /// StorageStandardServer = 0x00000015, /// /// Storage Server Workgroup /// StorageWorkgroupServer = 0x00000016, /// /// An unknown product /// Undefined = 0x00000000, /// /// Ultimate /// Ultimate = 0x00000001, /// /// Ultimate E /// UltimateE = 0x00000047, /// /// Ultimate N /// UltimateN = 0x0000001C, /// /// Web Server (full installation) /// WebServer = 0x00000011, /// /// Web Server (core installation) /// WebServerCore = 0x0000001D, /// /// Unlicensed or expired product /// Unlicensed = 0xABCDABCD, /// /// Home Server /// HomeServer = 0x00000013, /// /// Small Business Server Premium /// SmallBusinessServerPremium = 0x00000019, } } // **************************************************************************** // NEW CLASS - SHOULD BE PLACED IN SEPARATE FILE // **************************************************************************** /// /// This class contains Win32 functions used to get OS Version Info /// internal class NativeMethods { /// /// Prevents a default instance of the NativeMethods class from being /// created. /// private NativeMethods() { } /// /// This method retrives information about the current operating system /// /// Reference to the OSVersionInfoEx struct that /// receives the operating system information /// True if the function succeeds; otherwise false [DllImport("kernel32")] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool GetVersionEx(ref OSVersionInfoEx osvi); /// /// Retrieves the product type for the operating system on the local /// computer, and maps the type to the product types supported by the /// specified operating system. /// /// The major version number of the /// operating system. The minimum value is 6. /// The minor version number of the /// operating system. The minimum value is 0. /// The major version number of the /// operating system service pack. The minimum value is 0. /// The minor version number of the /// operating system service pack. The minimum value is 0. /// The product type. This parameter cannot be NULL. /// If the specified operating system is less than the current operating /// system, this information is mapped to the types supported by the /// specified operating system. If the specified operating system is /// greater than the highest supported operating system, this information /// is mapped to the types supported by the current operating system.If the /// product has not been activated and is no longer in the grace period, /// this parameter is set to PRODUCT_UNLICENSED (0xABCDABCD). Otherwise, /// this parameter can be one of the following values. /// True if the function succeeds; otherwise false. [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool GetProductInfo( int osMajorVersion, int osMinorVersion, int spMajorVersion, int spMinorVersion, ref uint type); /// /// Retrieves the specified system metric or system configuration setting. /// /// The system metric or configuration setting to be /// retrieved. /// A non-zero value if the function succeeds; otherwise 0. [DllImport("kernel32.dll")] internal static extern int GetSystemMetrics( int index); /// /// Contains operating system version information. The information includes /// major and minor version numbers, a build number, a platform identifier, /// and information about product suites and the latest Service Pack /// installed on the system. /// [StructLayout(LayoutKind.Sequential)] internal struct OSVersionInfoEx { /// /// The size of this data structure, in bytes /// public int VersionInfoSize; /// /// The major version number of the operating system /// public int MajorVersion; /// /// The minor version number of the operating system /// public int MinorVersion; /// /// The build number of the operating system /// public int BuildNumber; /// /// The operating system platform. This member should be /// VER_PLATFORM_WIN32_NT (2). /// public int PlatformId; /// /// A null-terminated string that indicates the latest Service Pack /// installed on the system. If no Service Pack has been installed, the /// string is empty. /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string CSDVersion; /// /// The major version number of the latest Service Pack installed on the /// system /// public Int16 ServicePackMajor; /// /// The minor version number of the latest Service Pack installed on the /// system /// public Int16 ServicePackMinor; /// /// A bit mask that identifies the product suites available on the system /// public Int16 SuiteMask; /// /// Any additional information about the system /// public byte ProductType; /// /// Reserved for future use /// public byte Reserved; } } }