我目前正在进行一个C#项目。我想收集用户统计数据以更好地开发软件。我正在使用 C# 的 Environment.OS
功能,但它只显示操作系统名称,例如 Microsoft Windows NT
我想要能够检索实际已知的操作系统名称,例如 Windows XP, Windows Vista 或 Windows 7
等等。
这种可能性存在吗?
我目前正在进行一个C#项目。我想收集用户统计数据以更好地开发软件。我正在使用 C# 的 Environment.OS
功能,但它只显示操作系统名称,例如 Microsoft Windows NT
我想要能够检索实际已知的操作系统名称,例如 Windows XP, Windows Vista 或 Windows 7
等等。
这种可能性存在吗?
添加 System.Management
的引用和 using 语句,然后执行以下操作:
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;
}
你应该尽量避免在本地使用WMI。虽然很方便,但会付出性能代价。可以将其视为懒惰税!
Kashish提供的关于注册表的答案并不适用于所有系统。下面的代码应该可以,并且还包括服务包:
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 "";
}
添加一个.NET引用到Microsoft.VisualBasic。然后调用:
new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName
来自 MSDN:
如果计算机上安装了 Windows Management Instrumentation (WMI),则此属性返回有关操作系统名称的详细信息。否则,此属性将返回与
My.Computer.Info.OSPlatform
属性相同的字符串,后者提供比 WMI 更少的信息。
My.Computer.Info.OSFullName
相同。 - ElektroStudiosString subKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion";
RegistryKey key = Registry.LocalMachine;
RegistryKey skey = key.OpenSubKey(subKey);
Console.WriteLine("OS Name: {0}", skey.GetValue("ProductName"));
我希望您会发现这个有用
System.OperatingSystem osInfo = System.Environment.OSVersion;
app.manifest
的 XML 文件,并取消注释 assembly\compatibility\application
下 Windows 10 的 <supportedOS>
行。然后您将获得实际的版本字符串,例如 Microsoft Windows NT 10.0.19043.0
。 - Gaborpublic 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;
}
public static string GetFriendlyOSName()
{
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/C systeminfo | findstr /c:\"OS Name\"";
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.WaitForExit();
string output = cmd.StandardOutput.ReadToEnd();
cmd.Close();
return output.Split(new[] { ':' }, 2)[1].Trim();
}
注意:
string text = (string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName");
这段代码将获取完整的操作系统名称,例如 "Windows 8.1 Pro"