我正在寻找一个优雅的方式来获取操作系统版本,如:“Windows XP Professional Service Pack 1”或“Windows Server 2008标准版”等。如何获得“友好”操作系统版本名称?
有没有一个优雅的方式做到这一点?
我也对处理器体系结构感兴趣(如x86或x64)。
我正在寻找一个优雅的方式来获取操作系统版本,如:“Windows XP Professional Service Pack 1”或“Windows Server 2008标准版”等。如何获得“友好”操作系统版本名称?
有没有一个优雅的方式做到这一点?
我也对处理器体系结构感兴趣(如x86或x64)。
您可以使用WMI来获取产品名称( “微软的WindowsServer®2008的企业”):
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";
有一点需要注意的是,这些信息通常是本地化的,并且根据操作系统的语言会有不同的报告。
您可以从WMI外观得到了很多信息,为Win32_OperatingSystem类
WMI似乎是未来的证明,因为它返回友好的名称开箱即用,没有任何转换... 将仔细研究。谢谢... – 2009-02-23 14:06:56
注意,处理器架构的问题是复杂的:
你的意思(高numers要求较低的数字是真实的):
,如果你是幸福的,所有3必须是真的,那么
IntPtr.Size == 8
表示所有三个都是真的
与“IntPtr.Size == 8”不一样吗? – 2009-02-23 13:51:03
我知道这个问题很复杂 - 正如你所说的那样。但我只是感兴趣哪个框架版本处理我的可执行文件。所以我的IntPtr方法就足够了。 – 2009-02-23 13:54:15
@Hosam:不完全。 IntPtr.Size是正确的做法。 – configurator 2009-02-23 14:39:52
为什么不使用Environment.OSVersion
?它还会告诉你这是什么操作 - Windows,Mac OS X,Unix等。要确定你是在64位还是在32位运行,使用IntPtr.Size
- 这将返回32位的4字节和64位的8字节。
`Environment.OSVersion`确实为您提供了hte操作系统名称的人类版本。例如,WMI将为您提供* Microsoft Windows 8.1 Pro *,`Environment.OSVersion`则提供* Microsoft Windows NT 6.2.9200.0 *。 – 2014-03-20 07:19:10
我发现`Environment.OSVersion`是不合适的,除非你有一个app.manafest文件声明支持的操作系统。否则,如果您的应用程序以Windows Vista而非Windows 10运行,则可能会得到完全错误的操作系统版本。 – 2015-07-31 16:42:33
输出示例:
Name = Windows Vista
Edition = Home Premium
Service Pack = Service Pack 1
Version = 6.0.6001.65536
Bits = 64
Sample类:
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/但是代码有错误,你将需要更换“案6”的声明(这只是#endregion名称前)与此:
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在本地使用。这是非常方便的,但你在性能方面付出了沉重的代价。这是简单而快捷:
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 "";
}
我知道这是没有直接回答这个问题,这也是一个有点晚了,但对于那些谁只是在寻找一种方法来确定是否OS是一个客户端操作系统或服务器存在使用以下方式:(您需要包括System.Management参考)
using System;
using System.Management;
ManagementClass osClass = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject queryObj in osClass.GetInstances())
{
foreach (PropertyData prop in queryObj.Properties)
{
if (prop.Name == "ProductType")
{
ProdType = int.Parse(prop.Value.ToString());
}
}
}
而可变ProdType是,在之前进行初始化的整数。它将包含1到3之间的值,而1代表Workstation,2代表域控制器,3代表服务器。
这是从Accessing the properties of Win32_OperatingSystem取出并改变了一点点......
小晚了,但是这是我做到了。未来可能会帮助别人。
using Microsoft.Win32;
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
string pathName = (string)registryKey.GetValue("productName");
尝试:
new ComputerInfo().OSVersion;
输出:
的Microsoft Windows 10企业
注: 添加引用到Microsoft.VisualBasic.Devices;
您可以使用Visual Basic设备获取版本信息。
代码:
using 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企业
WINNT
注: 您需要添加一个引用Microsoft.VisualBasic;
要小心,我见过很多这种打破代码示例当用户不是管理员...和当然很多代码示例也适用于非管理员用户。只是要保持谨慎^^ – 2009-02-23 14:08:08