tcarter2005
tcarter2005

Reputation: 617

Include/Exclude files per visual studio configuration

I have multiple folders in a visual studio project. I also have the same amount of configurations for this project. (One code base runs multiple sites).

I only want one folder per configuration to be included.

Right now, every configuration includes every single folder. Then, I have to manually delete the extra folders after build (if I so choose).

This also slows down our continuous integration builds, because all the extra files it has to process.

This cannot be achieved by the "Exclude From Project" / "Include In Project" context menu item. That would remove it from the whole project completely.

I need certain folders included only for certain configurations.

X Code (iOS devleopment) handles this by using "Target Memberships"... You can tell a certain file to only be included in a certain target.

Trying to do the same in VS.

Please help!!

Upvotes: 8

Views: 6043

Answers (1)

Shahzad Qureshi
Shahzad Qureshi

Reputation: 1805

You must manually edit your .csproj file, which is really just an MSBUILD file. (Right click, open in notepad or other text editor)

Here's an example. I duplicated the default Program.cs and created one for DEBUG and one for RELEASE.

Under normal conditions when I build this I would get an error because I have defined the same class twice.

But after editing the .csproj file as below, they now independently build depending on the configuration that I have selected.

  <ItemGroup>
    <Compile Include="DebugFiles\ProgramForDebug.cs" Condition=" '$(Configuration)' == 'Debug' "/>
    <Compile Include="ReleaseFiles\ProgramForRelease.cs"  Condition=" '$(Configuration)' == 'Release' "/>
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>

I might add, there is probably a better way to accomplish what you're doing. Perhaps using config transforms (maybe where different configurations specify the name of the class to use for your environment), but either way the above example will definitely accomplish what you're asking.

Upvotes: 20

Related Questions