How to have different solutions refer to one resx file?

If you're using a .resx file, you probably want to take advantage of the auto-generated code functionality that Visual Studio provides for .resx files. If you're including a .resx file in multiple projects, then you may want to have each project auto-generate its own code. You might do this because you want to use a different code generator for certain projects (ResXCodeFileGenerator vs. GlobalResourceProxyGenerator) or you might just want the namespace of the generated code to be aligned with the project. Here's how you can set that up.

I've set up a new solution with a C# console application project called SharedResx. I've also added a C# class library project to the solution called Resources, and within that project I added a new resource file named MySharedResource.resx. Visual Studio automatically creates a MySharedResource.Designer.cs file within my Resources project with the code inside of the Resources namespace.

First, include your existing .resx file in the SharedResx console application project as a link, as other answers have mentioned. Add Existing Item -> find ..\Resources\MySharedResource.resx -> use the drop down list on the Add button to select "Add as Link".

Next, you'll have to manually modify the project file to set up auto-generation. You can look at the Resources.csproj to see how the auto-generation is set up there as a guide for how it should look in SharedResx.csproj. Right-click on the SharedResx project and select Unload Project. Right-click again and select Edit SharedResx.csproj. Scroll down to find the EmbeddedResource element that corresponds to your linked .resx file:

<EmbeddedResource Include="..\Resources\MySharedResource.resx">
  <Link>MySharedResource.resx</Link>
</EmbeddedResource>

Modify this to include a Generator element and a LastGenOutput element:

<EmbeddedResource Include="..\Resources\MySharedResource.resx">
  <Link>MySharedResource.resx</Link>
  <Generator>ResXFileCodeGenerator</Generator>
  <LastGenOutput>SharedResx.MySharedResource.Designer.cs</LastGenOutput>
</EmbeddedResource>

Note: manual editing is only necessary if you want to control the name of the generated file as I've done in my example. Otherwise, you can specify the Generator element using the Custom Tool setting in the Properties window in Visual Studio.

Also note that in the LastGenOutput element I've named the generated file with the SharedResx project name as a prefix. This will cause the ResXFileCodeGenerator to create a file named SharedResx.MySharedResource.Designer.cs in the same folder as the .resx file. I've experimented with specifying a relative path rather than just a file name in the LastGenOutput element in order to get the generated file to be in a different folder, such as the SharedResx folder, but I found that it didn't work consistently. While I was able to generate the file in the correct location the first time, the LastGenOutput element lost its value so that subsequent generations would not target that same location. I gave up on that and just used the project name prefix as part of the file name in order to avoid possible conflicts with other projects.

Now, close the SharedResx.csproj file and right-click again to select Reload Project. Right-click on the linked MySharedResource.resx file in the SharedResx project and select Run Custom Tool. You should now see that a new linked file named SharedResx.MySharedResource.Designer.cs was added to the project as a nested file under the MySharedResource.resx file. You may have to turn on the "Show All Files" option in the Solution Explorer window in order to see it.

You now have a code file auto-generated from your shared .resx file included in your project.


Visual Studio also allows you to link to files instead of copying them. The feature is a bit hidden, but you can access it like this:

  1. Choose "add existing file"
  2. After selecting the file, do not double click but rather note the little dropdown arrow next to the "Open" button.
  3. Select "Link File" from the dropdown menu.