.NET – How to copy a file in Visual Studio based on the developer’s operating system architecture?

I am using Visual Studio 2015 to write a C# application. This application targets “Any CPU” (the “32-bit preferred” option is not enabled), which means the application will compile For a single build target that will run in 32-bit mode, a 32-bit operating system and a 64-bit operating system on a 64-bit operating system.

This application requires that a certain native DLL be copied to it Output folder (i.e. bin/Debug or bin/Release folder). This DLL has separate x86 and x64 versions, and the correct version needs to be copied to the output folder according to the developer’s operating system.

So far, I have found that I can conditionally copy files to the output folder by adding the following content to my .csproj file:



Always

So my question is, how do I write a condition equivalent to "The developer's operating system is x86" or "...x64"?

(To be very clear, I am not asking how to conditionally copy files on the platform build target, in my case it is always "any CPU". I am asking how to conditionally copy files based on the operating system architecture Copy the file which Visual Studio happens to be running.)

Thanks for some useful information on the original problem above The comments pointed me in the right direction, and I have figured out how to solve this problem:

I decided to copy the file in the post-build event and use the batch script command to check the PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 environment Variables. (More information about these variables here.)

The following is an example of how to do this in a post-build event:

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)"
)

According to Presumably, I can also use these environment variables in the .csproj file, as I did in the original question, but it seems easier and clearer for me to execute it in the post-build event, and I'm already in Use post-build to copy some other files.

I am using Visual Studio 2015 to write a C# application. This application targets "Any CPU" ("Preferred" is not enabled 32-bit” option), which means that the application will be compiled to 32-bit operating system and 64-bit mode on a single build target 64-bit operating system running in 2-bit mode.

This application requires that a certain native DLL be copied to its output folder (I.e. bin/Debug or bin/Release folder). This DLL has separate x86 and x64 versions, and the correct version needs to be copied to the output folder according to the developer's operating system.

So far , I have found that I can conditionally copy files to the output folder by adding the following content to my .csproj file:



Always

< p>So my question is, how do I write a condition equivalent to "The developer's operating system is x86" or "...x64"?

(To be very clear, I am not asking how to conditionally copy files on the platform build target, in my case it is always "any CPU". I am asking how to conditionally copy files based on the operating system architecture Copy the file which Visual Studio happens to be running.)

Thanks to some useful comments on the original question above for pointing me in the right direction, I have figured out How to solve this problem:

I decided to copy the file in the post-build event and use the batch script command to check the PROCESSOR_ARCHITECTURE and PROCESSOR_ARCHITEW6432 environment variables. (More information about these variables here. )

The following is an example of how to do this in a post-build event:

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 can also use these in .csproj files Environment variables, like I did in the original question, but it seems easier and clearer for me to execute it in a post-build event, and I'm already using post-build to copy some other files.

Leave a Comment

Your email address will not be published.