Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 989
tcarter2005 Avatar asked Mar 19 '15 20:03

tcarter2005


People also ask

What is exclude file?

If you exclude a file from a Web Site project (not Web Application), Visual Studio will simply add the . exclude extension to that file. If you remove the extension, that file will be included again. It's up to you if you still need those files or not.

How do I hide excluded files in Visual Studio?

Yes, there is a button to hide/show excluded files. The button is under solution explorer tab. Important Update: After trials on VS2012 I found out that if you create New Web Site the hide/show button will not appear for no apparent reason.


1 Answers

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.

like image 86
Shahzad Qureshi Avatar answered Sep 30 '22 15:09

Shahzad Qureshi