How my code can detect at runtime if it is compiled for x86 or for any CPU

I have many integration tests that read expected results from files. My tests access these files in relative ways. Relative paths are one level of depth, great for x86 vs Any CPU. For example, when my tests are run under x86, they need to read the following file "../../TestResults/MyTest.csv", but in the section "Any processor" they need to access "../TestResults/MyTest.csv"

So far, I have the following constant in every testing:

private const string platformDependentPrefix = ""; 

If I run my tests for x86, I need to manually change "to" .. / "in each test device.

Is there any way to automate it?

+4
source share
4 answers

Very hacky way, but it works:

 public static string Platform { get { if (IntPtr.Size == 8) return "x64"; else return "x86"; } } 

You can also access the CSharpProjectConfigurationProperties3.PlatformTarget property.

+5
source

Do you want the process to work as 64-bit or what are the compilation goals?

If you need the process bit, you can use the IntPtr.Size method mentioned by Teoman (or Environment.Is64BitProcess if you are using .NET 4).

If you want to use the target platform, I would look at Module.GetPEKind in the System.Reflection namespace. The PortableExecutableKinds out parameter will have different values โ€‹โ€‹depending on whether you are configured on x86, AnyCPU or x64 with the Required32Bit flag, without the flag set by the PE32Plus flag.

+4
source

You can define the โ€œbitnessโ€ at which the current process runs using IntPtr.Size. You will receive either 4 bytes (32-bit) or 8 bytes (64-bit). There is no such thing as working with any processor, but you can have #defines for this configuration that allow you to make decisions at compile time.

+3
source

You can add a conditional compilation symbol (Project-> Properties-> Build) to your project when you create it in X86 and use it to determine your path.

ex.

 #if X86 path = "x86 path"; #endif 

In addition to this, you can create a base test class in which all tests using this path are inherited. In this base class, you must use a compilation symbol. That way, you really only have to determine your path once.

+3
source

All Articles