问题
I am currently working on a C# project. I want to collect users statistics to better develop the software. I am using the Environment.OS
feature of C# but its only showing the OS name as something like Microsoft Windows NT
What I want to be able to retrieve is the actual known name of the OS like whether it is Windows XP, Windows Vista or Windows 7
and etc.
Is this possible?
回答1:
Add a reference and using statements for System.Management
, then:
public static string GetOSFriendlyName()
{
string result = string.Empty;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
foreach (ManagementObject os in searcher.Get())
{
result = os["Caption"].ToString();
break;
}
return result;
}
回答2:
You should really try to avoid WMI for local use. It is very convenient but you pay dearly for it in terms of performance. Think laziness tax!
Kashish's answer about the registry does not work on all systems. Code below should and also includes the service pack:
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 "";
}
回答3:
Add a .NET reference to Microsoft.VisualBasic. Then call:
new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName
From MSDN:
This property returns detailed information about the operating system name if Windows Management Instrumentation (WMI) is installed on the computer. Otherwise, this property returns the same string as the
My.Computer.Info.OSPlatform
property, which provides less detailed information than WMI can provide.information than WMI can provide.
回答4:
String subKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion";
RegistryKey key = Registry.LocalMachine;
RegistryKey skey = key.OpenSubKey(subKey);
Console.WriteLine("OS Name: {0}", skey.GetValue("ProductName"));
I hope that you find this useful
回答5:
System.OperatingSystem osInfo = System.Environment.OSVersion;
回答6:
public int OStype()
{
int os = 0;
IEnumerable<string> list64 = Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\SysWOW64"));
IEnumerable<string> list32 = Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\System32"));
if (list32.Count() > 0)
{
os = 32;
if (list64.Count() > 0)
os = 64;
}
return os;
}
来源:https://stackoverflow.com/questions/6331826/get-os-version-friendly-name-in-c-sharp