问题
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.
回答1:
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);
}
回答2:
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.
回答3:
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
.
回答4:
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...
回答5:
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);
来源:https://stackoverflow.com/questions/3138294/determining-if-the-program-is-running-on-windows-server