Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a resource conditionally in a CSPROJ

I am trying to embed a resource in my CSPROJ from two different directories, depending on the configuration. This post gave me the idea, but it's not working. 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 have also tried this but it worked equally bad.

<ItemGroup>
  <EmbeddedResource Include="..\$(Configuration)\file.txt">
    <Link>Files\file.txt</Link>
  </EmbeddedResource>
</ItemGroup>
like image 257
wpfwannabe Avatar asked Jan 08 '13 21:01

wpfwannabe


2 Answers

You only need to place the condition on 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>
like image 108
csharptest.net Avatar answered Nov 17 '22 06:11

csharptest.net


As I said in comments to your question this should work for you:

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

Even though you might see old values in "Full Path" of VS's property editor - when you build, it will respect your current configuration. VS Property Editor should be updated by Refresh button of Solution explorer or reloading project in the worst case. May be changing selection to other file and coming back to file.txt will be enough to refresh property editor.

UPDATE:

I've figured out in which case Full path changed for me by hitting "refresh" button of Solution explorer - it's Dll Reference's Hint path.

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

This will work only in case all DLLs (in all target folders) do actually exist.

For some reason for Item files Full path are not refreshed - for file items VS always think that current configuration named Debug - EVEN IF YOU DELETE DEBUG CONFIGURATION FROM THE PROJECT. Fortunately this VS Bug does not impact build - it still will take valid files.

like image 43
Philipp Munin Avatar answered Nov 17 '22 07:11

Philipp Munin