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
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
.
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...
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:
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);
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);
}
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.