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

后端 未结 9 1463
你的背包
你的背包 2020-12-16 04:04

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         


        
相关标签:
9条回答
  • 2020-12-16 04:39

    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";
    
    0 讨论(0)
  • 2020-12-16 04:44

    You can use Predefined Macros to set the properties on compilation

        #if (_WIN64) 
      const bool IS_64 = true;
    #else
      const bool IS_64 = false;
    #endif
    
    0 讨论(0)
  • 2020-12-16 04:51

    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.

    0 讨论(0)
  • 2020-12-16 04:56

    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
    
    0 讨论(0)
  • 2020-12-16 05:02

    There is nothing built in that will do this for you. You could always #define your own symbol and use that for conditional compilation if you wish.

    0 讨论(0)
  • 2020-12-16 05:03

    Can you do it at runtime?

    if (IntPtr.Size == 4)
      // 32 bit
    else if (IntPtr.Size == 8)
      // 64 bit
    
    0 讨论(0)
提交回复
热议问题