How to use the same settings in multiple projects in Visual Studio

When you want multiple projects in a solution to use the same project settings (some or all), you can put the settings in a shared props file.

There are two ways to do this:

  • Use Directory.Build.props. Add settings to this and they’ll automatically apply to all projects.
  • Use your own props file and import it into whichever projects you want.

I’ll show how to do these two options below.

Note: You can also use a combination of these two. Put settings that should go into all projects into Directory.Build.props, and then put settings that only need to go into some projects in your own props file and import it into those specific projects.

Option 1 – Use Directory.Build.props

You can use a special file called Directory.Build.props to automatically apply settings to all projects in a solution.

To do this option:

  • Add a file called Directory.Build.props to your solution’s root directory.
  • Add whatever project settings you want. Here’s an example:
<Project> <PropertyGroup> <Nullable>enable</Nullable> <NoWarn>CS8618</NoWarn> <AnalysisLevel>latest-recommended</AnalysisLevel> </PropertyGroup> </Project>
Code language: HTML, XML (xml)
  • I suggest you add the props file to the solution’s Solution Items for two reasons: 1) Be able to easily edit it in VS and 2) It helps you remember you’re using it (since it automatically applies settings just by existing in the directory hierarchy, which could be pretty confusing if you don’t realize you’re using it).

Now all projects in the solution will have these settings.

Settings are applied based on the directory hierarchy

So far I’ve oversimplified and said the settings apply to all projects in the solution. That’s true if you’re using the standard project structure – a solution directory with project subdirectories (and possibly organized into /src/ and /tests/ etc…).

To be more precise, settings from Directory.Build.props are applied to projects at or below it in the directory hierarchy. That’s why if you’re using the standard structure, you can simply stick it in the root directory and it applies to all projects.

This also means you can override settings for subsets of projects by sticking Directory.Build.props in their directory. For example, put it in the /tests/ directory to apply settings to test projects. If there are multiple Directory.Build.props in the directory hierarchy, it’ll only use the first “ancestor” it encounters for each project (it searches up the hierarchy, not down).

Override settings in a project

Settings specified in the csproj file override settings specified in Directory.Build.props.

Here’s an example. Let’s say you have the following Directory.Build.props file, enabling the Nullable feature for all projects:

<Project> <PropertyGroup> <Nullable>enable</Nullable> </PropertyGroup> </Project>
Code language: HTML, XML (xml)

Now let’s say you want to disable the Nullable feature in a specific project. You can simply specify this setting in the csproj file:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>disable</Nullable> </PropertyGroup> </Project>
Code language: HTML, XML (xml)

Because Nullable is specified in the csproj file, it overrides the Nullable setting from Directory.Build.props. The end result is the Nullable feature is disabled for this project.

Stated differently, you can’t use Directory.Build.props to override settings that are explicitly put in csproj. You can use your own custom props file to do that though, which I’ll show next.

Option 2 – Add your own props file

If you want better control over how settings are applied to multiple projects, you can add your own props file(s) and import them into each project.

To do this option:

  • Add a file to your solution root directory called Custom.props (or anything you want except for ‘Directory.Build.props’).
  • Add whatever project settings you want, like this:
<Project> <PropertyGroup> <Nullable>enable</Nullable> <NoWarn>CS8618</NoWarn> <AnalysisLevel>latest-recommended</AnalysisLevel> </PropertyGroup> </Project>
Code language: HTML, XML (xml)
  • Add this file as a Solution Item so you can easily edit it in VS.
  • To use this props file with a project, add an Import to the csproj:
<Project Sdk="Microsoft.NET.Sdk"> <!-- Import at the top = Don't override settings specified below (just like Directory.Build.props) --> <Import Project="../Custom.props"/> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <!-- Import at the bottom = Override settings specified above --> </Project>
Code language: HTML, XML (xml)

This imports the settings from Custom.props into this project.

Where you put the import statement makes a difference.

  • If you put it at the top (as shown), the existing settings in the csproj take precedence and won’t be overridden.
  • If you put it at the bottom, the settings from the props file take precedence and it overrides existing settings.

Use whichever option makes the most sense in your situation.

If the props file looks like plaintext in the VS editor

When you first add the props file and open it in Visual Studio, it may look like plaintext and not have IntelliSense support. This is probably because it didn’t have XML in it when it was first opened.

At a bare minimum, stick the following in the props file, then close and reopen it in the editor:

<Project> <PropertyGroup> <!-- Put project settings here --> </PropertyGroup> </Project>
Code language: HTML, XML (xml)

It should now have syntax coloring and IntelliSense support.

Leave a Comment