C# Directive to indicate 32-bit or 64-bit build
Solution 1
Can you do it at runtime?
if (IntPtr.Size == 4)
// 32 bit
else if (IntPtr.Size == 8)
// 64 bit
Solution 2
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
Solution 3
You can use a #if
directive and set the value as a compiler switch (or in the project settings):
#if VISTA64
...
#else
...
#endif
and compile with:
csc /d:VISTA64 file1.cs
when compiling a 64 bit build.
Solution 4
I am not sure if this is what you are looking for but I check the IntPtr.Size
to detect 32bit versus 64bit runtime. Note that this tells you the runtime environment, you might be running in WOW64
if (IntPtr.Size == 4)
{
//32 bit
}
else if (IntPtr.Size == 8)
{
//64 bit
}
else
{
//the future
}
Solution 5
What I use in my C# code is IntPtr.Size, it equals 4 on 32bit and 8 on 64bit:
string framework = (IntPtr.Size == 8) ? "Framework64" : "Framework";
coson
Updated on July 26, 2022Comments
-
coson almost 2 years
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?
-
Neil N almost 15 yearsIn reality, compiling for "Any CPU" while in 64 bit mode prevents you from deploying to 32 bit mode, unless MS fixed this bug in VS2008... but last I tried, you had to specify 32 bit.
-
Paul Alexander almost 15 years@Neil: I do all my development on a 64-bit machine and never seen this issue.
-
Neil N almost 15 yearsFrom an MS MVP: "the problem is deployment. MSI files / Deployment projects need to be targeted at one platform. This means if you want x86/x64 deployment, you compile your DLL's once, then build two deployment projects. This pattern also holds true for Merge Modules and such." eggheadcafe.com/…
-
Neil N almost 15 yearsI wouls actually say the problem is wider than he states there.. but not everyone will run into these issues.
-
Paul Alexander almost 15 yearsThat still only applies when you explicitly target specific platforms. The assemblies still target Any CPU. The MSI Project might specify 32/64-bit. In practice the MSI can simply be 32-bit and the app will still run 64-bit once installed.