以编程方式获取Windows OS版本

Pav*_*rov 10 c# windows operating-system version windowsversion

我试图在我的Windows 10机器上使用C#获取Windows版本.

我总是得到这些值(使用C#\ C++):

专业:6

轻微:2

哪个是Windows 8操作系统,相应于MSDN

C#代码:

var major = OperatingSystem.Version.Major
var minor  = OperatingSystem.Version.Minor
Run Code Online (Sandbox Code Playgroud)

C++代码

void print_os_info()
{
    //http://stackoverflow.com/questions/1963992/check-windows-version
    OSVERSIONINFOW info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOW));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);

    LPOSVERSIONINFOW lp_info = &info;
    GetVersionEx(lp_info);

    printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);
}
Run Code Online (Sandbox Code Playgroud)

Windows 10假设与那些:

专业:10

次要:0*

  • (当我从运行进程中获取转储文件时,我可以看到该文件的操作系统版本设置为10.0)

建造者:10.0.10586.0(th2_release.151029-1700)

我在这里错过了什么?

Spi*_*lis 14

在我的场景中,我需要我的应用程序来捕获可能的错误报告和统计信息的计算机信息.

我没有找到必须添加应用程序清单的解决方案.不幸的是,我在google搜索时发现的大多数建议都表明了这一点.

事实上,当使用清单时,必须手动添加每个操作系统版本,以便该特定操作系统版本能够在运行时报告自身.

换句话说,这成为竞争条件:我的应用程序的用户可能正在使用我的应用程序的版本,该应用程序在使用之前的操作系统之前.当微软推出新的操作系统版本时,我必须立即升级应用程序.我还必须强制用户在更新操作系统的同时升级应用程序.

换句话说,不太可行.

在浏览选项后,我发现了一些引用(与应用清单相比很少见)而是建议使用注册表查找.

我(砍掉)ComputerInfo类只WinMajorVersion,WinMinorVersionIsServer性质是这样的:

using Microsoft.Win32;

namespace Inspection
{
    /// <summary>
    /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
    /// </summary>
    public static class ComputerInfo
    {
        /// <summary>
        ///     Returns the Windows major version number for this computer.
        /// </summary>
        public static uint WinMajorVersion
        {
            get
            {
                dynamic major;
                // The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10, 
                // and will most likely (hopefully) be there for some time before MS decides to change this - again...
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
                {
                    return (uint) major;
                }

                // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint majorAsUInt;
                return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns the Windows minor version number for this computer.
        /// </summary>
        public static uint WinMinorVersion
        {
            get
            {
                dynamic minor;
                // The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10, 
                // and will most likely (hopefully) be there for some time before MS decides to change this - again...
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
                    out minor))
                {
                    return (uint) minor;
                }

                // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint minorAsUInt;
                return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns whether or not the current computer is a server or not.
        /// </summary>
        public static uint IsServer
        {
            get
            {
                dynamic installationType;
                if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
                    out installationType))
                {
                    return (uint) (installationType.Equals("Client") ? 0 : 1);
                }

                return 0;
            }
        }

        private static bool TryGetRegistryKey(string path, string key, out dynamic value)
        {
            value = null;
            try
            {
                using(var rk = Registry.LocalMachine.OpenSubKey(path))
                {
                    if (rk == null) return false;
                    value = rk.GetValue(key);
                    return value != null;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Nas*_*ine 5

您需要app.manifest在应用程序中添加一个:

在此处输入图片说明

在此处输入图片说明

然后取消注释以下行:

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
Run Code Online (Sandbox Code Playgroud)

  • 您仍然可以将清单添加到控制台应用程序@PavelDurov (2认同)
  • @PavelDurov使用`Environment.OSVersion.Version.Major`和`Environment.OSVersion.Version.Minor`在Windows 10上对我来说工作正常。 (2认同)

小智 5

Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuildNumber", string.Empty).ToString()
Run Code Online (Sandbox Code Playgroud)

从 XP 到当前 10.16299 的所有操作系统的相同代码,在 Windows 8 无法正常工作的情况下

OSVersion 属性报告 Windows 8 和 Windows 8.1 的相同版本号 (6.2.0.0) 以及 Windows 10 的相同主要和次要版本号。

https://msdn.microsoft.com/library/system.environment.osversion.aspx


Pet*_*aus 5

由于接受的答案仅适用于 C#,因此这里是 C++ 的解决方案。

它使用 ntdll.dll 中的 RtlGetVersion,该函数使用与 GetVersionEx 相同的结构(名称不同,但元素相同)并为您提供正确的版本。由于该函数通常用于驱动程序开发,因此该函数在DDK中声明,而不是在SDK中声明。所以我采用了动态的方案来调用该函数。请注意,ntdll.dll 在每次调用时都会加载和释放。因此,如果您更频繁地需要该功能,请保持加载库。

pOSversion 指向的结构必须像 GetVersionEx 一样进行初始化。

BOOL GetTrueWindowsVersion(OSVERSIONINFOEX* pOSversion)
{
   // Function pointer to driver function
   NTSTATUS (WINAPI *pRtlGetVersion)(
      PRTL_OSVERSIONINFOW lpVersionInformation) = NULL;

   // load the System-DLL
   HINSTANCE hNTdllDll = LoadLibrary("ntdll.dll");

   // successfully loaded?
   if (hNTdllDll != NULL)
   {
      // get the function pointer to RtlGetVersion
      pRtlGetVersion = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW))
            GetProcAddress (hNTdllDll, "RtlGetVersion");

      // if successfull then read the function
      if (pRtlGetVersion != NULL)
         pRtlGetVersion((PRTL_OSVERSIONINFOW)pOSversion);

      // free the library
      FreeLibrary(hNTdllDll);
   } // if (hNTdllDll != NULL)

   // if function failed, use fallback to old version
   if (pRtlGetVersion == NULL)
      GetVersionEx((OSVERSIONINFO*)pOSversion);

   // always true ...
   return (TRUE);
} // GetTrueWindowsVersion
Run Code Online (Sandbox Code Playgroud)