How to copy a file in Visual Studio depending on the architecture of the operating system of the developer?

I am writing a C # application using Visual Studio 2015. This application is for “any processor” (without the “Prefer 32-bit” option), which means that the application is compiled into one build target, which will run in 32-bit at 32 -bit operating systems and 64-bit mode in 64-bit operating systems.

For this application, you need to copy some native DLL into its output folder (i.e. bin / Debug or bin / Release folder). There are separate x86 and x64 versions of this DLL, and the correct copy must be copied to the output folder depending on the operating system of the developer.

Until now, I realized that I can copy files to the output folder conditionally by adding something like the following to the .csproj file:

<ItemGroup Condition="MY CONDITION HERE"> <Content Include="MyNativeLib.dll"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </Content> </ItemGroup> 

So my question is: how to write a condition equivalent to "developer operating system: x86" or "... x64"?

(To be extremely clear, I do not ask how to copy the file conditionally to the platform build target, which in my case is always “Any processor.” I ask how to copy the file conditionally depending on the OS on which Visual Studio is running.)

+5
source share
1 answer

Thanks to a few useful comments on the first question above, which pointed me in the right direction, I figured out how to solve this:

I decided to copy the file into the post-build event and use the batch script commands to check the PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 environment variables. (More on these variables here .)

Here is an example of how to do this in post-build events:

 set isX64=FALSE if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set isX64=TRUE if "%PROCESSOR_ARCHITEW6432%"=="AMD64" set isX64=TRUE if "%isX64%"=="TRUE" ( echo "Copying x64 dependencies..." copy "$(ProjectDir)Dependencies\x64\MyNativeLib.dll" "$(TargetDir)" ) ELSE ( echo "Copying x86 dependencies..." copy "$(ProjectDir)Dependencies\x86\MyNativeLib.dll" "$(TargetDir)" ) 

Presumably, I could also use these environment variables in the .csproj file, as I thought about it in the original question, but doing it in the post-build event was a little easier and more understandable, and I already used the -build post to copy some other files.

+1
source

All Articles