tags:

views:

56

answers:

2

Possible Duplicate:
How to detect Windows 64 bit platform with .net?

How do you know if the operating system is x64 or x86 from a c# .net 2.0 windows applicaiton?

Also the applicaiton is 32bit.

Thanks

+1  A: 

Use GetEnvironmentVariable to look for the PROCESSOR_ARCHITEW6432 variable. If it doesn't exist, you must be running 32bit:

bool is64bit = !string.IsNullOrEmpty(
    Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"));

EDIT:

Thanks to Hans Passant for pointing out the error in using the PROCESSOR_ARCHITECTURE variable.

GenericTypeTea
Have you actually tried this? Wow64 is not that easily defeated, emulation covers every corner. Except IsWow64Process and GetNativeSystemInfo.
Hans Passant
@Hans Passant - Copied from a production system. Should I be worried?
GenericTypeTea
Go ahead, worry.
Hans Passant
@Hans Passant - Was a serious question actually. I thought it pulled the value from 'HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE'? How can that be wrong?
GenericTypeTea
Registry virtualization is also taken care of by Wow64.
Hans Passant
@Hans Passant - Update the answer. Hopefully that's better.
GenericTypeTea
Yes, better, I know that one normally exists. What I'm not so sure about is that it is *guaranteed* to exist. Not close to an x64 machine right now to check, but avoid relying on registry keys that initialize the session. If the key is actually in the registry, rather than emulated by Wow64, it might not be there some day on some machine. The linked thread is a reliable way.
Hans Passant
@Hans Passant - Thank you for your advice, much appreciated :)
GenericTypeTea