Deploy a resource conditionally in CSPROJ

I am trying to embed a resource in my CSPROJ from two different directories, depending on the configuration. This post gave me this idea, but it does not work. Any help is appreciated.

<Choose> <When Condition="'$(Configuration)' == 'Debug'"> <ItemGroup> <EmbeddedResource Include="..\Debug\file.txt"> <Link>Files\file.txt</Link> </EmbeddedResource> </ItemGroup> </When> <Otherwise> <ItemGroup> <EmbeddedResource Include="..\Release\file.txt"> <Link>Files\file.txt</Link> </EmbeddedResource> </ItemGroup> </Otherwise> </Choose> 

I tried this too, but it worked equally badly.

 <ItemGroup> <EmbeddedResource Include="..\$(Configuration)\file.txt"> <Link>Files\file.txt</Link> </EmbeddedResource> </ItemGroup> 
+4
source share
2 answers

As I said in the comments on your question, this should work for you:

 <ItemGroup> <EmbeddedResource Include="..\$(Configuration)\file.txt"> <Link>Files\file.txt</Link> </EmbeddedResource> </ItemGroup> 

Even if you can see the old values ​​in the "Full path" of the VS property editor - when you create it, it will take into account your current configuration. The VS property editor should be updated with the Refresh button in Solution explorer or reload the project in the worst case. Maybe changing the selection to another file and returning to the .txt file will be enough to update the property editor.

UPDATE

I found out in which case the full path changed for me by clicking the Refresh button in Solution explorer - this is the path of the Dll Reference Hint link.

 <Reference Include="MyDll"> <SpecificVersion>False</SpecificVersion> <HintPath>..\$(Configuration)\MyDll.dll</HintPath> </Reference> 

This will only work if all the DLL files (in all target folders) really exist.

For some reason, Item files. The full path is not updated - for elements of a file, VS always thinks that the current configuration is named Debug - EVEN IF YOU REMOVE DEBUG CONFIGURATION FROM THE PROJECT. Fortunately, this VSBug does not affect the build - it will still accept valid files.

+3
source

You only need to set the condition in the ItemGroup element:

 <ItemGroup Condition="'$(Configuration)' == 'Debug'"> <EmbeddedResource Include="..\Debug\file.txt"> <Link>Files\file.txt</Link> </EmbeddedResource> </ItemGroup> <ItemGroup Condition="'$(Configuration)' == 'Release'"> <EmbeddedResource Include="..\Release\file.txt"> <Link>Files\file.txt</Link> </EmbeddedResource> </ItemGroup> 
+3
source

All Articles