Determining if the program is running on Windows Server

前端 未结 5 357
北恋
北恋 2020-12-03 04:06

I would like to determine if my program is running on a version of Windows Server. Apparently, System.Environment does not contain information about the fact th

相关标签:
5条回答
  • 2020-12-03 04:09

    IsWindowsServer is an inline function in VersionHelpers.h.

    You can find and read that header file on your computer. It uses the API function VerifyVersionInfoW.

    There is no function IswindowsServer in kernel32.dll.

    0 讨论(0)
  • 2020-12-03 04:16

    There's supposed to be a set of of 'Version Helper Functions' defined in the VersionHelpers.h header file of the WinAPI in assembly Kernel32.DLL. The one that, according to the documentation, should work for your case is IsWindowsServer(void). Description is here:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dn424963%28v=vs.85%29.aspx

    In c#, the code would like this (untested):

    using System.Runtime.InteropServices;
    
    public static class MyClass
    {
        [DllImport("Kernel32.dll")]
        public static extern Boolean IsWindowsServer();
    }
    

    And then the consumption code would simply be:

    bool is_it_a_server = MyClass.IsWindowsServer();
    

    I've never used any of these functions so let me know how it works...

    0 讨论(0)
  • 2020-12-03 04:18

    I had the same issue, albeit in scripting.

    I have found this value; I am querying it using WMI:

    https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx
    Win32_OperatingSystem
    ProductType
        Data type: uint32
        Access type: Read-only
        Additional system information.
        Work Station (1)
        Domain Controller (2)
        Server (3)
    

    I tested this for the following operating system versions:

    • Windows XP Professional SP3
    • Windows 7 Enterprise
    • Windows 8.1 Pro
    • Windows 8.1 Enterprise
    • Windows 10 Pro 10.0.16299
    • Windows Server 2003 R2 Standard Edition
    • Windows Server 2003 R2 Standard Edition x64
    • Windows Server 2008 R2 Standard
    • Windows Server 2012 Datacenter
    • Windows Server 2012 R2 Datacenter

    Find my example batch file below.

    Lucas.

    for /f "tokens=2 delims==" %%a in ( 'wmic.exe os get producttype /value' ) do (
        set PRODUCT_TYPE=%%a
    )
    if %PRODUCT_TYPE%==1 set PRODUCT_TYPE=Workstation
    if %PRODUCT_TYPE%==2 set PRODUCT_TYPE=DomainController
    if %PRODUCT_TYPE%==3 set PRODUCT_TYPE=Server
    echo %COMPUTERNAME%: %PRODUCT_TYPE%
    

    You can easily do this in C#:

    using Microsoft.Management.Infrastructure;
    ...
    string Namespace = @"root\cimv2";
    string className = "Win32_OperatingSystem";
    
    CimInstance operatingSystem = new CimInstance(className, Namespace);
    
    0 讨论(0)
  • 2020-12-03 04:22

    Thanks to pointers provided by Nick's answer, I've finally found what I was looking for. The function IsOS(OS_ANYSERVER) does exactly what I need. Here is the sample code which should work for any OS version (including pre-Vista, since we import the IsOS function by ordinal from shlwapi.dll):

    class OS
    {
        public static bool IsWindowsServer()
        {
            return OS.IsOS (OS.OS_ANYSERVER);
        }
    
        const int OS_ANYSERVER = 29;
    
        [DllImport("shlwapi.dll", SetLastError=true, EntryPoint="#437")]
        private static extern bool IsOS(int os);
    }
    
    0 讨论(0)
  • 2020-12-03 04:23

    You can p/invoke the following Win32 functions:

    GetProductInfo for Vista/Windows Server 2008+
    GetVersionEx for Windows 2000+

    BJ Rollison has a good post and sample code about this on his blog.

    0 讨论(0)
提交回复
热议问题