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 that Windows is a server version (there is no such info in the OS version object).
I know that I can use SystemInformation.TerminalServerSession
to check whether my program is running on a Remote Desktop (see also this question), but this will also be true if the user is simply accessing a plain client Windows machine remotely.
So is there a supported way of determining if the code is running on a server or on a client machine? I don't mind using P/Invoke if needed.
Note: I don't want to search for the "Server"
string in the product name, since this will probably not work on some systems because of the localization.
I had the same issue, albeit in scripting.
I have found this value; I am querying it using WMI:
I tested this for the following operating system versions:
Find my example batch file below.
Lucas.
You can easily do this in C#:
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.
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):
And then the consumption code would simply be:
I've never used any of these functions so let me know how it works...
IsWindowsServer
is an inline function inVersionHelpers.h
.You can find and read that header file on your computer. It uses the API function
VerifyVersionInfoW
.There is no function
IswindowsServer
inkernel32.dll
.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 fromshlwapi.dll
):