【问题标题】:Detect windows 2008 server and windows 2003 server in C#在 C# 中检测 windows 2008 server 和 windows 2003 server
【发布时间】:2011-03-29 18:33:22
【问题描述】:

谁能帮我修改此代码以支持 Windows 2003 和 Windows 2008 服务器? 谢谢

 public static string getOSLegacy()
       {
           //Get Operating system information.
           OperatingSystem os = Environment.OSVersion;
           //Get version information about the os.
           Version vs = os.Version;

           //Variable to hold our return value
           string operatingSystem = "";

           if (os.Platform == PlatformID.Win32Windows)
           {
               //This is a pre-NT version of Windows
               switch (vs.Minor)
               {
                   case 0:
                       operatingSystem = "95";
                       break;
                   case 10:
                       if (vs.Revision.ToString() == "2222A")
                           operatingSystem = "98SE";
                       else
                           operatingSystem = "98";
                       break;
                   case 90:
                       operatingSystem = "Me";
                       break;
                   default:
                       break;
               }
           }
           else if (os.Platform == PlatformID.Win32NT)
           {
               switch (vs.Major)
               {
                   case 3:
                       operatingSystem = "NT 3.51";
                       break;
                   case 4:
                       operatingSystem = "NT 4.0";
                       break;
                   case 5:
                       if (vs.Minor == 0)
                       {
                           operatingSystem = "2000";
                       }
                       else
                       {
                           operatingSystem = "XP";
                       }
                       break;
                   case 6:
                       if (vs.Minor == 0)
                       {
                           operatingSystem = "Vista";
                       }
                       else
                       {
                           operatingSystem = "7";
                       }
                       break;
                   default:
                       break;
               }
           }

           return operatingSystem;
       }

【问题讨论】:

    标签: c# operating-system


    【解决方案1】:

    在 Server 2003 上,版本返回 5.2.3790.131072。 在 Server 2008 上,版本返回 6.0.6002.131072。

    (在 Windows 7 上为 6.1.7600.0)。

    此外,您还可以从注册表中获取完整的操作系统名称:

    HKLM\Software\Microsoft\Windows NT\CurrentVersion,键 ProductName。

    【讨论】:

    • 我的 2008 返回 6.0,但我没有运行 R2。 R2 可能是 6.1。
    【解决方案2】:

    2003 年是 5.2。 2008 年是 6.1。

    这篇文章包含缺失的部分:http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5956c04f-072a-406c-ae6a-cc8b3a207936

    编辑: 如 cmets 所述,此答案不完整。 Slavik's answer 和链接的文章是一种更好的方法恕我直言。特别是,wProductType 字节(.NET API 中不存在)包含关键信息。

    【讨论】:

    • 这个答案很垃圾,因为 Windows 7 和 Server 2008 R2 具有相同的版本 (6.1),而 Win XP 64 位具有与 Windows Server 2003 相同的版本。
    • 同意。此答案包含虚假信息(以及链接代码):它指出 Win7 是 6.2,但 6.2 是 Win8
    【解决方案3】:

    Windows Server 2008 R2 与非 R2 不同。 在此处查看详细信息: http://www.codeproject.com/KB/miscctrl/OSVersionInfo.aspx

    我的检测 R2 的代码:

        public static bool Win7;
    
        [DllImport("kernel32.dll")]
        private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
    
        #region OSVERSIONINFOEX
        [StructLayout(LayoutKind.Sequential)]
        private struct OSVERSIONINFOEX
        {
            public int dwOSVersionInfoSize;
            public int dwMajorVersion;
            public int dwMinorVersion;
            public int dwBuildNumber;
            public int dwPlatformId;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string szCSDVersion;
            public short wServicePackMajor;
            public short wServicePackMinor;
            public short wSuiteMask;
            public byte wProductType;
            public byte wReserved;
        }
        #endregion OSVERSIONINFOEX
    
        static MyMethod () {
    
            Version ver = System.Environment.OSVersion.Version;
            OperatingSystem osVersion = Environment.OSVersion;
            OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
            osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
    
            GetVersionEx(ref osVersionInfo);
            byte productType = osVersionInfo.wProductType;
    
            if (ver.Major==6 & ver.Minor==1 & productType==1) {
                Win7=true;
                }
            else {
                Win7=false;
                }
    
            if (ver.Major==6 & ver.Minor==1 & productType==3)
                Report.Info ("OS is Windows Server 2008 R2");
            else //here standart methods can be used...
                Report.Info ("ver.Major: "+ver.Major.ToString()+"\r\nver.Minor: "+ver.Minor.ToString()+"\r\nproductType: "+productType.ToString());
    

    【讨论】:

      【解决方案4】:

      在 Win32 中,您使用 OSVERSIONINFOEX 的 wProductType 来确定这一点。奇怪的是,这些信息似乎无法通过 .NET 获得。也许它是我还没有找到的地方。无论如何,我都会担心依靠版本号来区分服务器操作系统和非服务器操作系统。

      【讨论】:

        【解决方案5】:

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

        我没有找到令人满意的解决方案,应用程序清单必须添加。我在谷歌搜索时发现的大多数建议都表明了这一点,很遗憾。 p>

        问题是,在使用清单时,必须手动将每个操作系统版本添加到其中,以便该特定操作系统版本能够在运行时报告自己。

        换句话说,这变成了一种竞争条件:我的应用程序的用户很可能正在使用我的应用程序的版本,该版本早于正在使用的操作系统。当 Microsoft 推出新的操作系统版本时,我必须立即升级应用程序。我还必须强制用户在更新操作系统的同时升级应用程序。

        换句话说,不太可行。

        浏览选项后,我发现了一些建议使用注册表查找的参考(与应用清单相比很少)。

        查看来自 MS 的关于映射到操作系统版本的图表:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx

        我的(切掉的)ComputerInfo 类只有 WinMajorVersionWinMinorVersionIsServer 属性如下所示:

        使用 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 (TryGeRegistryKey(@"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 (!TryGeRegistryKey(@"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 (TryGeRegistryKey(@"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 (!TryGeRegistryKey(@"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 (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
                            out installationType))
                        {
                            return (uint) (installationType.Equals("Client") ? 0 : 1);
                        }
        
                        return 0;
                    }
                }
        
                private static bool TryGeRegistryKey(string path, string key, out dynamic value)
                {
                    value = null;
                    try
                    {
                        var rk = Registry.LocalMachine.OpenSubKey(path);
                        if (rk == null) return false;
                        value = rk.GetValue(key);
                        return value != null;
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
        }
        

        【讨论】:

        • 您描述的竞争条件正是实现新行为的原因。除非您正在设计系统信息工具,否则操作系统版本是您不需要知道的信息。
        • @EKW,在我的场景中,我需要“系统信息”方面来报告用户的版本以进行许可证报告、统计和支持。我同意提供的信息可能不应该(ab)用于控制代码中关于使用或不使用哪些功能的逻辑。
        猜你喜欢
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 2014-02-12
        • 2010-09-12
        • 2010-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多