我正在尋找一個優雅的方式來獲取操作系統版本,如:「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