Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Directive to indicate 32-bit or 64-bit build

Is there some sort of C# directive to use when using a development machine (32-bit or 64-bit) that says something to the effect of:

if (32-bit Vista)
    // set a property to true
else if (64-bit Vista)
    // set a property to false

but I want to do this in Visual Studio as I have an application I'm working on that needs to be tested in 32/64 bit versions of Vista.

Is something like this possible?

like image 584
coson Avatar asked Jul 02 '09 20:07

coson


2 Answers

Can you do it at runtime?

if (IntPtr.Size == 4)
  // 32 bit
else if (IntPtr.Size == 8)
  // 64 bit
like image 54
marcc Avatar answered Sep 17 '22 13:09

marcc


There are two conditions to be aware of with 64-bit. First is the OS 64-bit, and second is the application running in 64-bit. If you're only concerned about the application itself you can use the following:

if( IntPtr.Size == 8 )
   // Do 64-bit stuff
else
   // Do 32-bit

At runtime, the JIT compiler can optimize away the false conditional because the IntPtr.Size property is constant.

Incidentally, to check if the OS is 64-bit we use the following

if( Environment.GetEnvironmentVariable( "PROCESSOR_ARCHITEW6432" ) != null )
    // OS is 64-bit;
else
    // OS is 32-bit
like image 21
Paul Alexander Avatar answered Sep 19 '22 13:09

Paul Alexander