Search code examples
c#visual-studio-2010csproj

Reusable PropertyGroup elements in a csproj file


I have a series of properties I need to set in ~15 projects. Is there a way to put these properties in a single file and have all the project files reference the one file using some sort of import directive rather than duplicating the properties in each project file?

EDIT: To clarify, I'm talking about <PropertyGroup> elements within the csproj file. All the projects need the same series of <PropertyGroup> settings. These elements set properties like DebugSymbols or DefineDebug, and are not used for referencing source files.


Solution

  • The <Import> element can be used for this, similar to how custom targets files are used.

    The reusable file should look like this:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <!-- Properties go here -->
        </PropertyGroup>
    </Project>
    

    Note that having the root Project element with the xmlns declaration is required - VS won't load a project referencing this file without it.

    I've saved my properties settings in my solution directory as ProjectBuildProperties.targets.

    To include the file in other projects, I've added this to the csproj files:

    <Import Project="$(SolutionDir)ProjectBuildProperties.targets"/>
    

    And it works!