Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically read target platform during run time

I want to know (for logging purposes) if the store application has been compiled for x86 / x64 / AnyCPU / Win32.

I couldn't find anything, but it seems that this information should be available during run time.

like image 911
David Božjak Avatar asked Sep 15 '25 11:09

David Božjak


2 Answers

Go to your project properties and for each platform add a Conditional compilation symbol (in Build tab). Let's say PLATFORM_X86, PLATFORM_X64 and PLATFORM_ANYCPU (you can also use /D option from command line).

You can use such symbols in your code:

#if PLATFORM_X86
    // Code specific for X86 builds
#endif

If you need to do it for logging you may simply declare a constant like:

#if PLATFORM_X86
private const string Platform = "X86";
#elif PLATFORM_X64
private const string Platform = "X64";
#elif PLATFORM_ANYCPU
private const string Platform = "AnyCPU";
#endif

Of course when compiling for AnyCPU you can use Environment.Is64BitProcess to know where you're running on.

like image 75
Adriano Repetti Avatar answered Sep 17 '25 01:09

Adriano Repetti


try in this way using IntPtr.Size

var result="";
if (IntPtr.Size == 8)
      result= "x64";
else
      result="x86";

the next method doesn't work in windows-store-apps as @Adriano Repetti flagged to me.
I don't delete it only for community reason.

another way is use \[Module.GetPEKind Method\]

 Assembly assembly = Assembly.GetExecutingAssembly();
 PortableExecutableKinds p;
 ImageFileMachine machineInfo;
 assembly .ManifestModule.GetPEKind(out p, out machineInfo);

after this line of code machineInfo variable should be one of:

  1. AMD64 for Targets a 64-bit AMD processor
  2. ARM for an ARM processor
  3. I386 for 32-bit Intel processor
  4. IA64 for 64-bit Intel processor
like image 37
faby Avatar answered Sep 17 '25 02:09

faby