How to have different solutions refer to one resx file?

后端 未结 7 2106
野趣味
野趣味 2021-02-04 09:47

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

相关标签:
7条回答
  • 2021-02-04 10:21

    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.

    0 讨论(0)
  • 2021-02-04 10:21

    One issue people are missing is that when you click the Add button when choosing the file to be added to the .resx file, you do NOT have the "add as link" option. The trick is to add the file (the one that is a resource for your project) first as a link to your project and making sure that the linked file is located in a folder named "Resources" that lives below the folder where your .resx file will live. Then, on your .resx file, choose add existing file and locate the file you originally linked in. Now, because Visual Studio will see that the file you are linking in is already a link in your project, it will now use the linked file when you build your project (and include and changes to that linked file on subsequent compiles down the road). (We used this technique back ~2005 using Visual Studio and I was able to to this again today using VS2019.

    0 讨论(0)
  • 2021-02-04 10:25

    One issue people are missing is that when you click the Add button when choosing the file to be added to the .resx file, you do NOT have the "add as link" option. The trick is to add the file (the one that is a resource for your project) first as a link to your project and making sure that the linked file is located in a folder named "Resources" that lives below the folder where your .resx file will live. Then, on your .resx file, choose add existing file and locate the file you originally linked in. Now, because Visual Studio will see that the file you are linking in is already a link in your project, it will use the linked when you build your project. (We accomplished this way back ~2005 using Visual Studio and I was able to to this again today using VS2019.

    0 讨论(0)
  • 2021-02-04 10:26

    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.
    0 讨论(0)
  • 2021-02-04 10:27

    I don't know why, but "Add as Link" is not working as expected for me, it still try to copy the file in the project folder (not good if you use Subversion Ankhsvn integration). Here I wrote a step by step guide with an approach similar to Dr. Wily's Apprentice one but with some enhancements, like real runtime sharing of the resources.

    0 讨论(0)
  • 2021-02-04 10:30

    In Visual Studio you have the ability to execute a script before the compilation, copy the file there.

    0 讨论(0)
提交回复
热议问题