2010-08-14 195 views
3

有人可以幫我修改此代碼以支持Windows 2003和Windows 2008服務器? 謝謝檢測Windows 2008服務器和Windows 2003服務器在C#

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

回答

1

2003是5.2。 2008年是6.1。

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

編輯:由評論指出這個答案是不完整的。 Slavik's answer和鏈接的文章是一個更好的方法恕我直言。特別是,wProductType字節(不存在於.NET API中)包含關鍵信息。

+3

由於Windows 7和Server 2008 R2具有相同版本(6.1),並且Win XP 64位與Windows Server 2003具有相同版本,所以此答案爲垃圾。 – Stian 2012-06-21 06:08:41

+1

同意。該答案包含虛假信息(以及鏈接的代碼):它指出Win7爲6.2,而Win8爲6.2 – Salaros 2013-01-26 18:46:25

2

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

(在Windows 7上它是6.1.7600.0)。

HKLM \ SOFTWARE \微軟\的Windows NT \ CURRENTVERSION,關鍵產品名稱:

此外,您還可以從註冊表中獲取完整的操作系統名稱。

+0

我的2008年返回6.0,但我沒有運行R2。 R2有可能是6.1。 – BillP3rd 2010-08-14 06:36:53

0

在Win32中,您可以使用OSVERSIONINFOEX的wProductType來確定這一點。這似乎很奇怪,這些信息似乎並不通過.NET。也許這是我找不到的地方。無論如何,依靠版本號來區分服務器和非服務器操作系統,我會感到緊張。

1

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

我的方案,我需要我的應用程序,以獲取可能的錯誤,報告和統計數據的計算機信息。

我沒有找到解決方案,其中應用程序清單必須添加滿意。不幸的是,我在Google上搜索時發現的大部分建議都表明了這一點。

當使用清單時,必須手動添加每個操作系統版本,以便特定操作系統版本能夠在運行時自行報告。

換句話說,這成爲一種競爭條件:我的應用程序的用戶很可能正在使用我的應用程序版本以前的日期正在使用的操作系統。當微軟啓動新的操作系統版本時,我將不得不立即升級應用程序。我還必須強制用戶在更新操作系統的同時升級應用程序。

換句話說,不是很可行。

在瀏覽選項後,我發現一些引用(與應用程序清單相比,出乎意料的少),而不是建議使用註冊表查找。

看看這個圖表從MS的問候映射OS版本: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; 
      } 
     } 
    } 
} 
+0

您描述的競爭條件正是新行爲實施的原因。除非您正在設計系統信息工具,否則操作系統版本是您不需要知道的信息。 – EKW 2016-10-18 19:24:51

+0

@EKW,在我的場景中,我需要「系統信息」方面報告用戶版本的許可證報告,統計信息和支持。我同意,雖然提供的信息可能不應該用於(ab)用於控制代碼中關於要使用或不使用哪些功能的邏輯。 – Spiralis 2016-10-20 08:54:45

相關問題