2014-03-30 120 views
2

如何检查我的.net应用程序是否在Windows 2003 Server上运行?操作系统是Windows服务器?

因为Buildnumber 5.2是Windows XP和还Windows Server 2003中

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

所以我需要一个不同的检查。 有人可以帮我吗?

+2

为什么,特别是,你需要检查?我认为XP和Server 2003基本相同(只要他们支持)。 –

+0

@Jesse好:我如何在vb.bet中执行此检查? – nexno

+0

我不太了解vb.net,但[这里是我找到的一个例子](https://gist.github.com/hdyk3118/6899110)。 –

回答

1

Environment.OSVersion只给你在XP和Server 2003中相同的内核版本,所以你不能真正区分它们。

但是,就我而言,它们在支持的功能方面几乎完全相同。如果你告诉我们为什么你需要知道差异,你想测试什么功能,我们可以帮助更多。

1

好吧伙计, 我编码的东西,应该检测如果操作系统是一个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服务器并检查是否返回真实与否。 I F你测试,请写出OS服务器版本号或姓名在这里,所以我知道它是否适合不同版本的^^

U可以检查这样的:

MsgBox(IsWindowsServer()) 
1

的最佳方式做这个IMO是使用WMI。 Win32_OperatingSystem类包含属性ProductType,其中1为工作站OS,2或3为服务器OS。

我在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(); 
     } 
    } 
} 
相关问题