在 C# 中获取操作系统版本/友好名称

发布于 2024-11-15 13:59:57 字数 252 浏览 5 评论 0原文

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

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

这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

红玫瑰 2024-11-22 13:59:57

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;
}

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;
}
萌能量女王 2024-11-22 13:59:57

您确实应该尽量避免在本地使用 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 "";
    }

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 "";
    }
日记撕了你也走了 2024-11-22 13:59:57

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

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

来自 MSDN:

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

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.

慈悲佛祖 2024-11-22 13:59:57
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"));

我希望你觉得这很有用

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

雨巷深深 2024-11-22 13:59:57
System.OperatingSystem osInfo = System.Environment.OSVersion;
System.OperatingSystem osInfo = System.Environment.OSVersion;
沉溺在你眼里的海 2024-11-22 13:59:57
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;
    }
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;
    }
难如初 2024-11-22 13:59:57

虽然这不是检测操作系统名称的完全 C# 方式,但下面的代码可以满足我的需求 -

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();
    }

注意:

  1. 此代码仅适用于 Windows 操作系统。
  2. 该代码仅在 Windows 10、11 上进行测试。

Though this is not a fully C# way of detecting the OS Name, below code works for my needs-

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();
    }

Note:

  1. This code works for only Windows OS.
  2. The code is tested only on Windows 10, 11.
烟火散人牵绊 2024-11-22 13:59:57
string text = (string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName");

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

string text = (string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName");

this code will get the full os name like this "Windows 8.1 Pro"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文