views:

603

answers:

3

Is there a #define in C# that allows me to know, at compile time, if I'm compiling for x86 (Win32) or x64 (Win64)?

A: 

Not that I know of. You would have to declare your own in different project configurations.

Marc Gravell
A: 

As far as I know Visual Studio defines only DEBUG and TRACE constants. Instead of declaring such constant manually in the project configurations you could use NANT to build your project. It can determine the build platform at compile time and define a custom directive accordingly.

Darin Dimitrov
+2  A: 

By default there is no way to do this. The reason is that C# code is not designed to target a particular platform as it runs on the CLR.

It is possible to hand roll this though. You can use the project configuration settings in Visual Studio to define your own constants. Or if you want it a little more streamline you can edit the .csproj yourself and hand roll some more configurations which have various defines.

For instance you can make your project file look like the following. I removed some of the information to make the x86/amd64 information clear.

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <!-- ... -->
    <DefineConstants>TRACE;DEBUG;X86</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|amd64' ">
    <!-- ... -->
    <DefineConstants>TRACE;DEBUG;AMD64</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

Adding this to a .csproj file gives me 2 new platform configurations in my project.

JaredPar