Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my code detect at run time if it compiled for x86 or for Any CPU

I have a lot of integration tests which read expected results from files. My tests access those files by relative paths. Relative paths are one level of depth different for x86 vs Any CPU. For example, when my tests run under x86, they need to read the following file "../../TestResults/MyTest.csv", but under Any CPU they need to access "../TestResults/MyTest.csv"

So far I have the following constant in every test fixture:

   private const string platformDependentPrefix = "";

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

Is there a way to automate it?

like image 433
Arne Lund Avatar asked Jul 05 '11 19:07

Arne Lund


People also ask

How can we determine at runtime when the code is executed If we are on a 32-bit or 64-bit architecture?

Checking at run time if you are running a 64-bit application: To see whether your process is 64-bit or 32-bit you can simply check the IntPtr. Size or any other pointer type. If you get 4 then you are running on 32-bit.

What is the difference between any CPU and x86?

"Any CPU" means that when the program is started, the . NET Framework will figure out, based on the OS bitness, whether to run your program in 32 bits or 64 bits. There is a difference between x86 and Any CPU: on a x64 system, your executable compiled for X86 will run as a 32-bit executable.

Should I target x86 or x64?

Since x86 apps run on both x64 and x86 systems, the most compatible choice is to build x86. If you MUST build a 64 bit solution, you'll need to target x64 and use our x64 dlls.

How to run Visual Studio in 64bit?

From the Visual Studio menu, choose Test, then choose Processor Architecture for AnyCPU projects. Choose x64 to run the tests as a 64-bit process.


1 Answers

Very hacky way but works:

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

Also you can access the CSharpProjectConfigurationProperties3.PlatformTarget property.

like image 71
Teoman Soygul Avatar answered Sep 27 '22 20:09

Teoman Soygul