2

I'm trying to make Visual Studio build system to copy a file to the output directory for a C# project. This file is both platform (x86/x64) and configuration (Debug/Release/...) dependent. For instance I have different files for Debug|x64 and Release|x64 builds. The way I wanted to handle this was by adding a file to the project in the Solution Explorer and manually editing the project file. For example, if I wanted to copy the file named myfile.dat to the output directory, I would write the following set of macros in the C# project file:

<ItemGroup>
    <Content Include="$(SolutionDir)\data\$(Configuration)\$(Platform)\myfile.dat">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

My idea was that the file myfile.dat would be copied from one of the subfolders in the data folder, depending on the selected build type.

Solution folder
|
+--data
   |
   +--Release
   |  |
   |  +--x86
   |  |
   |  +--x64
   |
   +--Debug
      |
      +--x86
      |
      +--x64

But for some reason the $(Platform) macro always evaluates to AnyCPU, when it's part of the file reference path. Actually there are two macros $(Platform) and $(PlatformName), and they both evaluate to AnyCPU. I coulnd't find any documentation describing the difference between these macros. I also tried to evaluate these macros in the Pre-build event and it gave the correct result then. I understand that I can use Pre-build event to copy the file, I'm just curious why it doesn't work as part of the file reference path. It there another part of the project file that I need to change for this to work?

UPDATE

PS. Project platform is set to values either x86 or x64. In fact the AnyCPU platform is deleted from the Configuration Manager.

Max
  • 19,654
  • 13
  • 84
  • 122
  • Maybe a stupid question, but have you checked the settings in the Configuration Manager (accessible through right-click on solution node in Solution Explorer)? – Lucero Mar 22 '16 at 17:20
  • Do you mean project properties? I did change the platform of course. – Max Mar 22 '16 at 17:23
  • I mean exactly what I wrote...! The project platform can be configured for each solution platform. Now if you change the solution platform, this does not necessarily affect the project platform unless it's set up correctly in the Configuration Manager. – Lucero Mar 22 '16 at 17:35
  • A bit dated but still pretty accurate: https://blogs.msdn.microsoft.com/aaronhallberg/2007/06/25/solution-configurations/ – Lucero Mar 22 '16 at 17:38
  • Yes, I made sure that the platform is not `AnyCPU`. In fact I have deleted the `AnyCPU` platform completely from the configuration manager. – Max Mar 22 '16 at 17:41
  • What happens if you build that project alone, from the commandline, using `msbuild my.csproj /t:Build /p:Platform=x86`? – stijn Mar 22 '16 at 17:45
  • @stijn. It doesn't copy anything when I build project file, but it works correctly when I build the solution. – Max Mar 22 '16 at 18:04

2 Answers2

1

$(Platform) is evaluating to AnyCPU probably because your project Properties>>Build>>Platform is set to AnyCPU. Note that this is different than the Platform target. You can create a new platform configuration from Build>>Configuration Manager>>New with the correct platform name and settings.

Below are similar SO questions about the same thing.

How can you access the Visual Studio solution level platform from a C# project's build event?

Visual Studio macro/anything else to iterate through all projects and set project properties

Community
  • 1
  • 1
Lithium
  • 373
  • 7
  • 21
1

$(PlatformTarget) is what you need. It returns the proper AnyCPU, x86 or x64. Make sure you test for the AnyCPU condition, because it's the same as x86. For example:

Condition=" '$(PlatformTarget)' == 'x86' Or '$(PlatformTarget)' == 'AnyCPU' "

synkro
  • 414
  • 7
  • 9