[英]In C#, how can I know programmatically if the Operating system is x64 or x86
In C#, how can I know programmatically if the Operating system is x64 or x86 在C#中,如何以编程方式了解操作系统是x64还是x86
I found this API method on the internet, but it doesn't work 我在互联网上找到了这种API方法,但它不起作用
[DllImport("kernel32.dll")]
public static extern bool IsWow64Process(System.IntPtr hProcess, out bool lpSystemInfo);
public static bool IsWow64Process1
{
get
{
bool retVal = false;
IsWow64Process(System.Diagnostics.Process.GetCurrentProcess().Handle, out retVal);
return retVal;
}
}
Thanks in advance. 提前致谢。
In .NET 4.0 you can use the new Environment.Is64BitOperatingSystem property. 在.NET 4.0中,您可以使用新的Environment.Is64BitOperatingSystem属性。
And this is how it's impemented 这就是它的成功
public static bool Is64BitOperatingSystem
{
[SecuritySafeCritical]
get
{
bool flag;
return ((Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && Win32Native.IsWow64Process(Win32Native.GetCurrentProcess(), out flag)) && flag);
}
}
Use reflector or similar to see exactly how it works. 使用反射器或类似物来确切了解它是如何工作的。
bool x86 = IntPtr.Size == 4;
If you build against AnyCPU, and you run on a 64-bit system, it will run on the 64-bit version of the framework. 如果您针对AnyCPU构建,并且您在64位系统上运行,它将在64位版本的框架上运行。 On a 32-bit system, it will run on the 32-bit version of the framework.
在32位系统上,它将在32位版本的框架上运行。 You can use this to its advantage by simply checking the
IntPtr.Size
property. 只需检查
IntPtr.Size
属性,就可以使用它。 If the Size
= 4, you are running on 32-bit, Size
= 8, you are running on 64-bit. 如果
Size
= 4,则运行在32位, Size
= 8,您运行的是64位。
Have a look at this: 看看这个:
http://msdn.microsoft.com/en-us/library/system.environment_members.aspx http://msdn.microsoft.com/en-us/library/system.environment_members.aspx
I think you are looking for System.Environment.OSVersion 我认为您正在寻找System.Environment.OSVersion
The following is from this answer , so don't upvote me for it :) 以下是这个答案 ,所以不要赞成我:)
if (8 == IntPtr.Size
|| (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
// x64
}
else
{
// x86
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.