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 options:
- Use Directory.Build.props.
- Add/import a custom props file.
I’ll show both options below.
Note: You can also use a combination of these two options.
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:
- It can be easily edited in VS.
- It helps you remember you’re using it.
Now all projects in the solution will have these settings.
Directory.Build.props applies to projects below it in the hierarchy
Settings from Directory.Build.props are applied to projects at or below it in the directory hierarchy. So add it to the root directory so it applies to all projects.
Override settings in a project
There are two ways to override settings from Directory.Build.props in a project:
- Add the settings to the csproj file.
- Add a Directory.Build.props in the project’s directory.
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)
To override this setting in the project, add it to 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)
The end result is the Nullable feature is disabled for this project.
Option 2 – Add your own props file
You can add your own props file(s) and import them into each project. This gives you better control over which settings are applied to your project. It’s more explicit than using Directory.build.props.
To add a props file, add a file called Custom.props to the root directory and add settings. Here’s an example:
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<NoWarn>CS8618</NoWarn>
<AnalysisLevel>latest-recommended</AnalysisLevel>
</PropertyGroup>
</Project>
Code language: HTML, XML (xml)
Tip: Add this file as a Solution Item so you can edit it in VS.
Now import Custom.props in a project:
<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)
Where you put the import statement makes a difference.
- Put it at the top so it doesn’t override existing settings in csproj.
- Put it at the bottom to override settings in csproj.
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.