Programmatically read target platform during run time

别等时光非礼了梦想. 提交于 2020-04-14 08:10:31

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/25035510/programmatically-read-target-platform-during-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!