我正在寻找一种优雅的方式来获得操作系统版本:"Windows XP Professional Service Pack 1"或"Windows Server 2008 Standard Edition"等.
这样做有一种优雅的方式吗?
我也对处理器架构感兴趣(如x86或x64).
您可以使用WMI获取产品名称("Microsoft®WindowsServer®2008Enterprise"):
using System.Management; var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast() select x.GetPropertyValue("Caption")).FirstOrDefault(); return name != null ? name.ToString() : "Unknown";
您应该尽量避免WMI供本地使用.这是非常方便的,但你在性能方面付出了巨大的代价.这很简单:
public string HKLM_GetString(string path, string key) { try { RegistryKey rk = Registry.LocalMachine.OpenSubKey(path); if (rk == null) return ""; return (string)rk.GetValue(key); } catch { return ""; } } public string FriendlyName() { string ProductName = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName"); string CSDVersion = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CSDVersion"); if (ProductName != "") { return (ProductName.StartsWith("Microsoft") ? "" : "Microsoft ") + ProductName + (CSDVersion != "" ? " " + CSDVersion : ""); } return ""; }
为什么不用Environment.OSVersion
?它还会告诉你这是什么操作--Windows,Mac OS X,Unix等.要查明你是在64位还是32位运行,请使用IntPtr.Size
- 这将返回4位为32位,8位为64位.
尝试:
new ComputerInfo().OSVersion;
输出:
Microsoft Windows 10企业版
注意:
添加引用Microsoft.VisualBasic.Devices;
样本输出:
Name = Windows Vista Edition = Home Premium Service Pack = Service Pack 1 Version = 6.0.6001.65536 Bits = 64
样本类:
class Program { static void Main( string[] args ) { Console.WriteLine( "Operation System Information" ); Console.WriteLine( "----------------------------" ); Console.WriteLine( "Name = {0}", OSInfo.Name ); Console.WriteLine( "Edition = {0}", OSInfo.Edition ); Console.WriteLine( "Service Pack = {0}", OSInfo.ServicePack ); Console.WriteLine( "Version = {0}", OSInfo.VersionString ); Console.WriteLine( "Bits = {0}", OSInfo.Bits ); Console.ReadLine(); } }
OSInfo类的源代码:http://www.csharp411.com/determine-windows-version-and-edition-with-c/但是代码中有错误,您需要替换"case 6"语句(就在#endregion NAME之前)这个:
case 6: switch (minorVersion) { case 0: switch (productType) { case 1: name = "Windows Vista"; break; case 3: name = "Windows Server 2008"; break; } break; case 1: switch (productType) { case 1: name = "Windows 7"; break; case 3: name = "Windows Server 2008 R2"; break; } break; } break;
如果你想更进一步,看看你的程序是以64位还是32位运行:
public static class Wow { public static bool Is64BitProcess { get { return IntPtr.Size == 8; } } public static bool Is64BitOperatingSystem { get { // Clearly if this is a 64-bit process we must be on a 64-bit OS. if (Is64BitProcess) return true; // Ok, so we are a 32-bit process, but is the OS 64-bit? // If we are running under Wow64 than the OS is 64-bit. bool isWow64; return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64; } } static bool ModuleContainsFunction(string moduleName, string methodName) { IntPtr hModule = GetModuleHandle(moduleName); if (hModule != IntPtr.Zero) return GetProcAddress(hModule, methodName) != IntPtr.Zero; return false; } [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] extern static IntPtr GetCurrentProcess(); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] extern static IntPtr GetModuleHandle(string moduleName); [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] extern static IntPtr GetProcAddress(IntPtr hModule, string methodName); }