【问题标题】:OS is Windows Server?操作系统是 Windows Server?
【发布时间】:2014-03-30 21:00:28
【问题描述】:

如何检查我的 .net 应用程序是否在 Windows 2003 Server 上运行?

因为 Buildnumber 5.2 是 Windows XP 和 Windows Server 2003。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx

所以我需要一张不同的支票。 有人可以帮我吗?

【问题讨论】:

标签: c# vb.net operating-system version


【解决方案1】:

Environment.OSVersion 只为您提供与 XP 和 Server 2003 相同的内核版本,因此您无法真正区分它们。

但是,就我而言,它们支持的功能几乎相同。如果您告诉我们您为什么需要了解其中的区别、您想测试什么功能,我们或许可以提供更多帮助。

【讨论】:

    【解决方案2】:

    好吧,伙计们, 我编写了一些代码来检测操作系统是否是 Windows 服务器。 如果是这样,它应该返回 true:

        Private ReadOnly Property IsWindowsServer() As Boolean
        Get
            Const VER_NT_WORKSTATION As Byte = &H1
            Const VER_PRODUCT_TYPE As UInteger = &H80
            Const VER_EQUAL As Byte = 1
            Dim osvi As New OSVERSIONINFOEX()
            osvi.dwOSVersionInfoSize = CUInt(Marshal.SizeOf(osvi))
            osvi.wProductType = VER_NT_WORKSTATION
            Dim dwlConditionMask As ULong = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL)
            Return Not VerifyVersionInfo(osvi, VER_PRODUCT_TYPE, dwlConditionMask)
        End Get
    End Property
    
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Structure OSVERSIONINFOEX
        Public dwOSVersionInfoSize As Integer
        Public dwMajorVersion As Integer
        Public dwMinorVersion As Integer
        Public dwBuildNumber As Integer
        Public dwPlatformId As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
        Public szCSDVersion As String
        Public wServicePackMajor As UInt16
        Public wServicePackMinor As UInt16
        Public wSuiteMask As UInt16
        Public wProductType As Byte
        Public wReserved As Byte
    End Structure
    
    <DllImport("kernel32.dll")> _
    Private Function VerSetConditionMask(dwlConditionMask As ULong, dwTypeBitMask As UInteger, dwConditionMask As Byte) As ULong
    End Function
    
    <DllImport("kernel32.dll")> _
    Private Function VerifyVersionInfo(<[In]> ByRef lpVersionInfo As OSVERSIONINFOEX, dwTypeMask As UInteger, dwlConditionMask As ULong) As Boolean
    End Function
    

    我需要有人可以在不同的 Windows 服务器上检查它并检查它是否返回 true。 如果您测试,请在此处填写 OS-Server Build Number 或 Name,以便我知道它是否适用于不同版本 ^^

    你可以这样检查:

    MsgBox(IsWindowsServer())
    

    【讨论】:

      【解决方案3】:

      执行此 IMO 的最佳方法是使用 WMI。 Win32_OperatingSystem 类包含一个属性 ProductType,对于工作站操作系统,该属性为 1,对于服务器操作系统,该属性为 2 或 3。

      我不擅长VB.NET,也许其他人可以为你转换这个C#:

      public static bool IsServerOS()
      {
          return IsServerOS(Environment.MachineName);
      }
      public static bool IsServerOS(string computerName)
      {
          ConnectionOptions options = new ConnectionOptions() { EnablePrivileges = true, Impersonation = ImpersonationLevel.Impersonate };
          ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\CIMV2", computerName), options);
          ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
      
          using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
          using (ManagementObjectCollection results = searcher.Get())
          {
              if (results.Count != 1) throw new ManagementException();
      
              uint productType = (uint)results.OfType<ManagementObject>().First().Properties["ProductType"].Value;
      
              switch (productType)
              {
                  case 1:
                      return false;
                  case 2:
                      return true;
                  case 3:
                      return true;
                  default:
                      throw new ManagementException();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-12-10
        • 1970-01-01
        • 2015-10-25
        • 2016-08-19
        • 2011-11-17
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多