Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have different solutions refer to one resx file?

I have one resx file and I want to use it from several solutions\projects , and i don't want to have local copy at each solution(only at compile time bring a copy).

Is there some way to do so ? Because when i add it as "Add existing file" it copies me a copy locally.

Any idea how to do so ?

like image 793
Night Walker Avatar asked Nov 16 '10 17:11

Night Walker


People also ask

Do RESX files need to be deployed?

You do not need to deploy . resx file with your application.

How do I read a RESX file?

To start with, you need to create a web application. Here, I am using Visual Studio 2012 and C# as the language. Once you created the application, you need to create a RESX file by clicking the “New Item”. Now you can see a new file in your solution explorer named Resource1.

Are RESX files compiled?

resx files are compiled into binary . resources files.


2 Answers

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.

like image 77
Dr. Wily's Apprentice Avatar answered Oct 25 '22 22:10

Dr. Wily's Apprentice


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.
like image 43
Heinzi Avatar answered Oct 25 '22 23:10

Heinzi