18

Is there a way to conditionally embed resources into a .NET project? I.e. if I have defined INCLUDETHIS then I want a certain large file embedded into the dll, otherwise I do not want it embedded. I know I can make two projects to do this, but I'm hoping to do it all in one project. The code that uses the file is easily conditioned with a #if INCLUDETHIS ... #endif, but I don't even want the file in the dll otherwise as it contains sensitive information and is only used internally.

jjxtra
  • 20,415
  • 16
  • 100
  • 140

2 Answers2

23

This did the trick. You can edit your csproj by right clicking in Visual Studio on the project and selecting "edit".

  <Choose>
    <When Condition=" '$(Configuration)'!='ReleaseExternal' And '$(Platform)'=='x86' ">
      <ItemGroup>
        <EmbeddedResource Include="file.dll">
          <Link>Resources\Other\file.dll</Link>
        </EmbeddedResource>
      </ItemGroup>
    </When>
  </Choose>
jjxtra
  • 20,415
  • 16
  • 100
  • 140
  • You can edit csproj in Visual Studio, but you have to unload that project first. – Sarrus Jul 21 '16 at 09:33
  • I've always been able to edit with notepad++ while it's loaded – jjxtra Jul 21 '16 at 12:00
  • I mean It's not neccecary to use external text editor – Sarrus Jul 21 '16 at 18:40
  • I wondered how you did compound conditions in the `Condition` attribute. I cannot find documentation on how the syntax for the `Condition` attribute (unless there is some sorta universal syntax that I don't know). – Michael Plautz Mar 08 '18 at 05:14
  • @MichaelPlautz it is standarized for MSBuild see https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions and https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditional-constructs – Sarrus May 07 '18 at 13:46
1

You can conditionally embed the resource in your csproj-file depending on configuration, however I'm not sure if you're able to control it using #if statements in your code.

Maybe add a custom build-configuration instead (in addition to "Debug" and "Release")? And then just manually edit your csproj-file to embed it if $(Configuration == 'your custom buildconf').

jishi
  • 24,126
  • 6
  • 49
  • 75