Visual Studio – Share a file between multiple projects

When you want to share a file between multiple projects, instead of duplicating the file’s contents, you can add a link to the existing file. You can link any type of file, but it’s most commonly done with config / source files. Here’s an example of adding appsettings.json as a linked file:

  • In the project where you want to add the linked file: right-click the project > Add > Existing Item.
  • Navigate to the existing file.
  • Click on the dropdown arrow next to the Add button and choose Add As Link.
Visual Studio Add Existing Item dialog with "Add As Link" dropdown option selected.

Now you’ll see the file twice – the original file in one project and the linked file in the other project.

Visual Studio - Two projects pointing to the same appsettings.json file (physical in one, linked in the other)

These are logical files pointing to one physical file. You can edit the file within any project and the changes will be reflected everywhere. Note: You can tell it’s a linked file because it has a little arrow on the icon.

Copy linked content file to the build directory

When you add a linked content file (such as appsettings.json), you’ll want to configure it to get copied to the build directory:

  • Right-click the linked appsettings.json file > Properties.
  • Set Copy To Output Directory = Copy if newer.

Now when you build, it’ll copy the linked file to the build directory.

Linked file in .csproj

Here’s what a linked file looks like in .csproj:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> </PropertyGroup> <ItemGroup> <Content Include="..\WebAPI\appsettings.json" Link="appsettings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> </Project>
Code language: HTML, XML (xml)

Notice two things about the highlighted line:

  • Include=”..\WebAPI\appsettings.json” is the relative path to the physical file.
  • Link=”appsettings.json” is the linked file’s logical name in the project.

You can make the logical file name different from the physical file’s name if you want, but be aware that it uses the logical name when copying to the build directory. For example, if you set Link=”test.json”, it’ll copy the physical appsettings.json file to the output directory using name test.json.

In future projects, if you don’t want to add linked files through the UI, you can copy and paste the linked file XML to the new .csproj. Just make sure the relative path (Include=”…”) is correct for the new project you’re adding.

Where should the physical file go?

Where you put the physical file is a personal preference. There are two main approaches for this:

  • Put the physical file in one project directory, and add linked files in all the other projects. Example of this is shown above.
  • Put the physical file in the solution directory, add it as an item at the solution level, and add linked files in all projects.

Here’s an example of the solution directory approach:

Visual Studio - appsettings.json added at the solution level with linked files added in the two projects

Leave a Comment