27

我目前正在做一个 C# 项目。我想收集用户统计数据以更好地开发软件。我正在使用Environment.OSC# 的功能,但它仅将操作系统名称显示为Microsoft Windows NT之类的名称

我希望能够检索的是操作系统的实际已知名称,例如它是否是Windows XP, Windows Vista or Windows 7等等。

这可能吗?

4

7 回答 7

57

为 添加引用和使用语句System.Management,然后:

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;
}
于 2011-06-13T14:35:34.040 回答
12

你真的应该尽量避免 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 "";
    }
于 2013-11-28T01:16:17.947 回答
9

添加对 Microsoft.VisualBasic 的 .NET 引用。然后调用:

new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName

来自MSDN

如果计算机上安装了 Windows Management Instrumentation (WMI),则此属性返回有关操作系统名称的详细信息。否则,该属性返回与该属性相同的字符串,该字符串My.Computer.Info.OSPlatform提供的详细信息比 WMI 所能提供的要少。信息比 WMI 所能提供的要少。

于 2012-12-06T12:49:13.790 回答
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"));

我希望你觉得这很有用

于 2013-08-27T21:11:34.637 回答
3
System.OperatingSystem osInfo = System.Environment.OSVersion;
于 2011-06-13T14:33:54.347 回答
2
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;
    }
于 2015-11-30T20:51:18.747 回答
-1
string text = (string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName");

此代码将获得完整的操作系统名称,如“Windows 8.1 Pro”

于 2021-10-03T17:33:45.717 回答