开发者

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

I found this API method on the internet, but it doesn't work

[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.

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. On a 32-bit system, it will run on the 32-bit version of the framework. You can use this to its advantage by simply checking the IntPtr.Size property. If the Size = 4, you are running on 32-bit, Size = 8, you are running on 64-bit.


Have a look at this:

http://msdn.microsoft.com/en-us/library/system.environment_members.aspx

I think you are looking for 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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜