1

I'm using C# in Visual Studio 2017. As shown below, I'm using the .csproj file to embed a DLL, so that the executable contains the DLL inside itself.

  <ItemGroup>
    <EmbeddedResource Include="example.dll">
      <LogicalName>example.dll</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

For automation purposes, I'm also building the project using MSBuild in the CLI. Thing is, I want the DLL embedding code as shown above in one of the releases, but not in the other.

I've found three ways to go about this, but the first doesn't seem possible, the second seems unwise and the third is just ugly:

Idea 1: Creating new solution configurations and then assigning conditional compilation symbols to them, followed by the usual #if() and #endif() commands within the code. Unfortunately, such conditional compilation does not work on the .csproj file.

Idea 2: Creating dual .sln and .csproj files for the SAME project. This way, I can edit the second .sln file to point to a second .csproj which doesn't contain the <ItemGroup> code as shown above. I tested the idea and it appears to work, but I feel manually hand-editing such fundamental files to create seconds versions of them is risky.

Idea 3: I really don't want to do this as it breaks one of the main programming tenets - unnecessary duplication: Maintain and update two completely separate solutions/projects. Every time I adjust something in one project, I have to adjust it in the other project too. Yuck.

Is there a way to make Idea 1 work, is Idea 2 safe, and are there any alternatives?

Dan W
  • 3,520
  • 7
  • 42
  • 69
  • This post has been marked as duplicate. The alternative post at the top by the moderators is good, but it misses out a key piece of info, which is how to set the property from MSBuild within the CLI, but I added a comment there to clarify. – Dan W Nov 30 '18 at 05:02
  • This might be closer to what you are looking for than the linked duplicate: https://stackoverflow.com/q/28387596/517852 – Mike Zboray Nov 30 '18 at 06:33
  • @MikeZboray: That looks good too, but I was able to get away with simply adding `/p:USEDLL=true` (or false) to the MSBuild command (no need for DefineConstants) – Dan W Nov 30 '18 at 06:54

0 Answers0