27

私は現在C#プロジェクトに取り組んでいます。ソフトウェアをより良く開発するために、ユーザーの統計を収集したいと思います。私はC#の機能を使用していますが、OS名はMicrosoftWindowsNTEnvironment.OSのようなものとしてしか表示されません

取得できるようにしたいのは、OSの実際の既知の名前(それがそうであるかどうかWindows XP, Windows Vista or Windows 7など)です。

これは可能ですか?

4

7 に答える 7

57

の参照とusingステートメントを追加し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");

このコードは、この「Windows8.1Pro」のような完全なOS名を取得します

于 2021-10-03T17:33:45.717 に答える