2014-03-30 118 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(); 
     } 
    } 
} 
相關問題