我正在寻找一种优雅的方法来获取操作系统版本,例如:"Windows XP Professional Service Pack 1"或"Windows Server 2008 Standard Edition"等。
是否有一种优雅的方法可以实现这一点?
我还对处理器架构(如x86或x64)感兴趣。
我正在寻找一种优雅的方法来获取操作系统版本,例如:"Windows XP Professional Service Pack 1"或"Windows Server 2008 Standard Edition"等。
是否有一种优雅的方法可以实现这一点?
我还对处理器架构(如x86或x64)感兴趣。
你可以使用 WMI 获取产品名称(“Microsoft® Windows Server® 2008 Enterprise”):
using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast<ManagementObject>()
select x.GetPropertyValue("Caption")).FirstOrDefault();
return name != null ? name.ToString() : "Unknown";
FirstOrDefault
方法代替First
方法,否则会抛出异常。原因是在空集合上调用First
方法会导致异常。 - franzaCast<T>
而不是OfType<T>
也可以略微提高性能。 - Derek W你应该尽量避免在本地使用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 "";
}
Registry.LocalMachine.OpenSubKey(path)
更改为 Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path);
,并传入计算机名称。您还需要使用 ServiceController sc = new ServiceController("RemoteRegistry", computer); if (sc.Status.Equals(ServiceControllerStatus.Running)) { ... // do your stuff }
来检查远程注册表服务是否正在运行,并且如果未运行,则可以启动它:sc.Start();
。 - vapcguyEnvironment.OSVersion
?它还会告诉你操作系统是什么 - Windows,Mac OS X,Unix等等。要查找当前运行的是64位还是32位,请使用 IntPtr.Size
- 这将返回32位的4个字节和64位的8个字节。Environment.OSVersion
可以获得操作系统的版本信息,但返回的是计算机可读的版本号码,和人们常用的操作系统名称不同。例如,WMI 可以返回 Microsoft Windows 8.1 Pro 这样的具体名称,而 Environment.OSVersion
返回的则是 Microsoft Windows NT 6.2.9200.0 这样的编码格式。 - Sean KearonEnvironment.OSVersion
不太合适,除非你有一个声明支持的操作系统的 app.manifest 文件。否则,如果你的应用程序在 Windows Vista 上运行而不是 Windows 10,你可能会得到完全错误的操作系统版本。 - Adam VeneziaEnvironmen.OSVersion
。 - NexX尝试:
new ComputerInfo().OSVersion;
输出:
Microsoft Windows 10 企业版
注意:
添加对 Microsoft.VisualBasic.Devices;
的引用。
new ComputerInfo().OSFullName
的输出是操作系统的全名。 - andySFMicrosoft Windows 10.0.18362
。System.Runtime.InteropServices.RuntimeInformation.OSDescription
有点晚了,但这是我做的方法。可能会帮助未来的某个人。
using Microsoft.Win32;
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
string pathName = (string)registryKey.GetValue("productName");
registryKey = Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion"); string version = registryKey.GetValue("productName").ToString();
- vapcguystring servicePack = rk.GetValue("CSDVersion").ToString();
- vapcguy示例输出:
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);
}
需要注意的是,这些信息通常是本地化的,并且会根据操作系统语言而有所不同。
您可以从WMI中获取大量信息,查找Win32_OperatingSystem类。
IntPtr.Size == 8
表示三个条件都为真
IntPtr.Size == 8
吗? - Hosam Alyusing Microsoft.VisualBasic.Devices;
var versionID = new ComputerInfo().OSVersion;//6.1.7601.65536
var versionName = new ComputerInfo().OSFullName;//Microsoft Windows 7 Ultimate
var verionPlatform = new ComputerInfo().OSPlatform;//WinNT
Console.WriteLine(versionID);
Console.WriteLine(versionName);
Console.WriteLine(verionPlatform);
输出:
6.1.7601.65536
Microsoft Windows 10 Enterprise
WinNT
注意:
您需要添加对 Microsoft.VisualBasic;
的引用。
10.0.x.x
。而6.1.7601.x
对应的是Windows 7。从这里能够得到的最大值是6.2.9200.0
,它是Windows 8(.1)。 - Vertigo