Search code examples
c#visual-studiobuildconfiguration

How to exclude specific files from compiling in a certain build configuration in C# solutions


In my Visual Studio 2022 C# solution I have two solution configurations Debug A and Debug B. I want to disable configuration Debug B from compiling certain files (or folders). That is, I want config Debug A to build all project files but config Debug B to not build certain files.

How can this be achieved in the solution? I have different precompile macros in the different configs, which I could use to manually exclude all the code in the files, but I wonder if there is a better way?

The files I want to exclude are not in a specific project and hence they cannot be excluded in the configuration manager. I also tried to set the Build Action to None in the file settings but this seems to stay the same for both configurations.


Solution

  • You can use conditions, both within the code itself and in the .csproj to perform different operations depending on some types of build conditions, such as framework when it is multi framework, type of build, etc...

    I trust this would work for you:

    <ItemGroup Condition=" '$(Configuration)' == 'Debug A' ">
      <ItemGroup>
        <Folder Exclude="folder\" />
      </ItemGroup>
    </ItemGroup>
    

    Microsoft Docs: How to exclude files form the build

    I found a thread with the same question, here's: Another topic with same question