6

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>
Community
  • 1
  • 1
wpfwannabe
  • 14,587
  • 16
  • 78
  • 129
  • This should work: . Why you say it's not working? What are the actual results when you use it? If you think it does not work because of old values in property editor in VS, then refresh it or reload project - it was working for me – Philipp Munin Jan 08 '13 at 21:39
  • AFAIR, the "Full Path" in the properties does not change when I switch configurations. – wpfwannabe Jan 08 '13 at 21:41
  • Hit refresh button in solution explorer, or reopen solution/project after changing configuration. Even if you build project having old values in Property editor - it should respect new values and build properly. – Philipp Munin Jan 08 '13 at 21:43

2 Answers2

5

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>
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
  • Hm... I have tried this and it doesn't work. Actually, the Properties of the embedded file aren't updated. I am not sure whether the build picks up the correct file though. Need to check that. – wpfwannabe Jan 08 '13 at 21:46
  • I am assuming this probably works too. I've chosen the other answer because it's simpler and serves my purpose well. Still, many thanks and +1. – wpfwannabe Jan 08 '13 at 21:53
  • Is it ok to let this stay? `` Or do you also need the condition for this part? – testing Aug 27 '20 at 11:27
3

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.

Philipp Munin
  • 5,610
  • 7
  • 37
  • 60
  • Thanks! So, I was on the right track from the beginning. It is the unmodified "Full Path" that got me off the track. This solution does work but in my VS 2012 nothing changes the "Full Path" (none of your proposed tricks). Still, this is the least of my worries if the embedding works correctly. – wpfwannabe Jan 08 '13 at 21:52
  • Just provided important update, please see - it may help you to investigate workaround, please post it here if you find it – Philipp Munin Jan 09 '13 at 00:53
  • This is worth reporting to Microsoft. Other than this, I am satisfied that that approach works. The value of "Full Path" although quite misleading is really of minor importance. – wpfwannabe Jan 09 '13 at 09:11