【发布时间】:2011-09-14 17:00:54
【问题描述】:
- 如何确定Windows的版本? WinXP、Vista 或 7 等。
- 32 位还是 64 位?
UPD:适用于 .Net 2.0 - 3.5
【问题讨论】:
-
请注意检查 32 位或 64 位,因为您的进程可能与操作系统的位数不同
UPD:适用于 .Net 2.0 - 3.5
【问题讨论】:
您正在寻找Environment.OSVersion、Environment.Is64BitProcess 和Environment.Is64BitOperatingSystem 属性。
.Net 4.0之前,可以通过IntPtr.Size是否为8来判断进程是否为64位,通过@987654321可以判断操作系统是否为64位@:
public static bool Is64BitProcess
{
get { return IntPtr.Size == 8; }
}
public static bool Is64BitOperatingSystem
{
get
{
// Clearly if this is a 64-bit process we must be on a 64-bit OS.
if (Is64BitProcess)
return true;
// Ok, so we are a 32-bit process, but is the OS 64-bit?
// If we are running under Wow64 than the OS is 64-bit.
bool isWow64;
return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
}
}
static bool ModuleContainsFunction(string moduleName, string methodName)
{
IntPtr hModule = GetModuleHandle(moduleName);
if (hModule != IntPtr.Zero)
return GetProcAddress(hModule, methodName) != IntPtr.Zero;
return false;
}
[DllImport("kernel32.dll", SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
extern static IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
【讨论】:
OperatingSystem 类型直接不公开字段来确定位数。我认为,需要对已知术语进行一些验证。
Is64BitOperatingSystem
Environment 拥有这样的房产。好东西。这一定是 .NET4 的新内容?
【讨论】:
看看Environment.OSVersion和Environment.Is64BitOperatingSystem
【讨论】: