C# 重新启动安装windows updateEnvironment.OSVersion后,此过程会为您提供hte OS名称的人工版本。例如,WMI将为您提供Microsoft Windows 8.1 Pro,Environment.OSVersion为Micr

C# 重新启动安装windows updateEnvironment.OSVersion后,此过程会为您提供hte OS名称的人工版本。例如,WMI将为您提供Microsoft Windows 8.1 Pro,Environment.OSVersion为Micr,c#,.net,winforms,operating-system,C#,.net,Winforms,Operating System,重新启动安装windows updateEnvironment.OSVersion后,此过程会为您提供hte OS名称的人工版本。例如,WMI将为您提供Microsoft Windows 8.1 Pro,Environment.OSVersion为Microsoft Windows NT 6.2.9200.0。我发现Environment.OSVersion不合适,除非您有一个说明支持的操作系统的app.manafest文件。否则,如果您的应用程序以Windows Vista而不是Windows


重新启动安装windows update
Environment.OSVersion
后,此过程会为您提供hte OS名称的人工版本。例如,WMI将为您提供Microsoft Windows 8.1 Pro,
Environment.OSVersion
为Microsoft Windows NT 6.2.9200.0。我发现
Environment.OSVersion
不合适,除非您有一个说明支持的操作系统的app.manafest文件。否则,如果您的应用程序以Windows Vista而不是Windows 10运行,您可能会得到完全错误的操作系统版本,例如,如果您为x86平台编译.NET应用程序,即使在64位操作系统上运行,IntPtr.Size也将返回4。更好的解决方案是:从configurator的链接:“从Windows 8开始,OSVersion属性为所有Windows平台返回相同的主版本号和次版本号。因此,我们不建议您检索此属性的值以确定操作系统版本。”我有Windows 8.1,但它显示Windows Vista。。。通过查看源代码,甚至没有Windows7或Windows8的案例说明…@TheMuffinMan-可能是因为那是2.5年前写的?当新版本的windows问世时,您必须对其进行更新。如果它像这样简单,为什么人们会这么多?获取本地计算机操作系统的“友好”名称就是这么简单。当涉及到操作系统版本号时,它可能会变得更加复杂,而这并不是我们所问的问题。@NateS,这并不是问题所在。他在受苦!注意:这只在本地计算机上有效。您必须将
Registry.LocalMachine.OpenSubKey(path)
更改为
Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine,computer).OpenSubKey(path)
使其成为真正的WMI替换(通常用于连接到远程计算机),并传入
计算机
。您还需要使用
ServiceController sc=newservicecontroller(“RemoteRegistry”,computer)检查远程注册表服务是否正在运行;如果(sc.Status.Equals(ServiceControllerStatus.Running)){…//do your stuff}
&可以启动它,如果不能:
sc.start()
new ComputerInfo()。OSFullName
提供该输出。
OSVersion
,正如您所期望的,返回的是版本,而不是名称。使用
OSFullName
获取名称。这太棒了。或者将其用于远程计算机:
registryKey=Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine,computer).OpenSubKey(“软件\\Microsoft\\Windows NT\\CurrentVersion”);字符串版本=registryKey.GetValue(“productName”).ToString()此外,为了满足OP的要求,服务包位于相同的位置,不同的字符串:
string servicePack=rk.GetValue(“CSDVersion”).ToString()虽然我可以理解在一些项目中添加VisualBasic引用时的犹豫,但我的范围和上下文对我来说一点也不麻烦。这对我来说是一个很好的解决方案。这似乎是Microsoft文档中
环境的官方替代方案。OSVersion
,请参阅
IntPtr.Size == 8
using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast<ManagementObject>()
                      select x.GetPropertyValue("Caption")).FirstOrDefault();
return name != null ? name.ToString() : "Unknown";
Name = Windows Vista
Edition = Home Premium
Service Pack = Service Pack 1
Version = 6.0.6001.65536
Bits = 64
class Program
{
    static void Main( string[] args )
    {
        Console.WriteLine( "Operation System Information" );
        Console.WriteLine( "----------------------------" );
        Console.WriteLine( "Name = {0}", OSInfo.Name );
        Console.WriteLine( "Edition = {0}", OSInfo.Edition );
        Console.WriteLine( "Service Pack = {0}", OSInfo.ServicePack );
        Console.WriteLine( "Version = {0}", OSInfo.VersionString );
        Console.WriteLine( "Bits = {0}", OSInfo.Bits );
        Console.ReadLine();
    }
}
case 6:
    switch (minorVersion)
    {
        case 0:

            switch (productType)
            {
                case 1:
                    name = "Windows Vista";
                    break;
                case 3:
                    name = "Windows Server 2008";
                    break;
            }
            break;
        case 1:
            switch (productType)
            {
                case 1:
                    name = "Windows 7";
                    break;
                case 3:
                    name = "Windows Server 2008 R2";
                    break;
            }
            break;
    }
    break;
public static class Wow
{
    public static bool Is64BitProcess
    {
        get { return IntPtr.Size == 8; }
    }

    public static bool Is64BitOperatingSystem
    {
        get
        {
            // Clearly if this is a 64-bit process we must be on a 64-bit OS.
            if (Is64BitProcess)
                return true;
            // Ok, so we are a 32-bit process, but is the OS 64-bit?
            // If we are running under Wow64 than the OS is 64-bit.
            bool isWow64;
            return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
        }
    }

    static bool ModuleContainsFunction(string moduleName, string methodName)
    {
        IntPtr hModule = GetModuleHandle(moduleName);
        if (hModule != IntPtr.Zero)
            return GetProcAddress(hModule, methodName) != IntPtr.Zero;
        return false;
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    extern static IntPtr GetCurrentProcess();
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    extern static IntPtr GetModuleHandle(string moduleName);
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
}
    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 "";
    }
        using System;
        using System.Management;

        ManagementClass osClass = new ManagementClass("Win32_OperatingSystem");
        foreach (ManagementObject queryObj in osClass.GetInstances())
        {

            foreach (PropertyData prop in queryObj.Properties)
            {

                if (prop.Name == "ProductType")
                {

                    ProdType = int.Parse(prop.Value.ToString());
                }
            }
        }
using Microsoft.Win32;

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
        string pathName = (string)registryKey.GetValue("productName");
new ComputerInfo().OSVersion;
using Microsoft.VisualBasic.Devices;

var versionID = new ComputerInfo().OSVersion;//6.1.7601.65536
var versionName = new ComputerInfo().OSFullName;//Microsoft Windows 7 Ultimate
var verionPlatform = new ComputerInfo().OSPlatform;//WinNT

Console.WriteLine(versionID);
Console.WriteLine(versionName);
Console.WriteLine(verionPlatform);
string SimpleOSName()
{
    var name = new ComputerInfo().OSFullName;
    var parts = name.Split(' ').ToArray();
    var take = name.Contains("Server")?3:2;
    return string.Join(" ", parts.Skip(parts.IndexOf("Windows")).Take(take));
}
string SimpleOSName()
{
    var name = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem")
        .Get().Cast<ManagementObject>()
        .Select(x => x.GetPropertyValue("Caption").ToString())
        .First();
    var parts = name.Split(' ').ToArray();
    var take = name.Contains("Server")?3:2;
    return string.Join(" ", parts.Skip(parts.IndexOf("Windows")).Take(take));
}
System.Runtime.InteropServices.RuntimeInformation.OSDescription