问题
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:
AMD64
for Targets a 64-bit AMD processorARM
for an ARM processorI386
for 32-bit Intel processorIA64
for 64-bit Intel processor
来源:https://stackoverflow.com/questions/25035510/programmatically-read-target-platform-during-run-time