Changing Resource files (resx) namespace and access modifier

C#Visual Studio-2008NamespacesResx

C# Problem Overview


In my webproject I'm using 4 resources files in my App_GlobalResources folder. One of them (lang.resx) has been created before my arrival on the project. It has the correct namespace (WebApplication.App_GlobalResources) and access modifier : public.

On the other hand, the three others Resource files that I just created have a different namespace (Resources) and internal access modifier, and I can't change it on the Resource File Form from Visual Studio because it's disabled. If I try to change it directly in the designer.cs file, the modifications are cancelled on the next save of the file.

It's not a critical bug but it can be misleading for the others developers on the project to find different namespaces and access modifiers for the resources files they will use.

C# Solutions


Solution 1 - C#

The quick answer is: Just open the Properties of the resource file and change the Custom Tool Namespace to the namespace you need.
Simple as that.

Solution 2 - C#

I'm not entirely sure where the problem lies yet, but I can tell you that you can solve it by changing the tool used to generate the code.

When I tried to follow this article, I also stumbled onto this problem. After downloading the source files as the author suggested, I noticed that the resource file that were already present had the following class in the "Custom Tool" property: "PublicResXFileCodeGenerator". Also, the "Build Action" property was set to "Embedded Resource", but I'm not sure if that's part of the problem.

Any new resource file that I created used the custom tool "GlobalResourceProxyGenerator". After overwriting this with the aforementioned "PublicResXFileCodeGenerator" seemed to solve the problem, whatever the real problem may be.

I also noticed that the present resource file was in the "2.0" format, whereas new files were in the "1.3" format. You can see this when you open the resx file using an XML editor (or by using "open with" from visual studio itself).

I hope you can make it work like this, it's not ideal though. It's likely to be an installation issue with Visual Studio 2008 and SP1, or something like that.

Update:

This blog entry may also help.

Solution 3 - C#

Or you can change the CustomTool attribute (tested in VS2010).

> You just have to open the file properties of the resource file and > change the “Custom Tool” from “GlobalResourceProxyGenerator” to > “PublicResXFileCodeGenerator”, which is the default Tool for local > resource files. Next you have to change the “Build Action” to > “Embedded Resource”. You may also want to assign a proper Custom Tool > Namespace like “Resources” in order to access the file properly, but > this isn’t necessary...

Source

Solution 4 - C#

The resx picks up the namespace depending on the namespace specified in your Visual Studio project configuration. Update your project to have the right namespace, and the resx should inherit it (new ones for sure, not sure if existing ones will be fixed - they should).

Solution 5 - C#

Resource Files access modifiers are in the .csproj;

Changing directly the .csproj file should workaround this problem.

Look for the <Generator> element and set its value in accordance to the examples below:

A resource file with internal modifier looks like this,

<ItemGroup>
 <EmbeddedResource Update="resources.resx">
  <Generator>ResXFileCodeGenerator</Generator>
  <LastGenOutput>resources.Designer.cs</LastGenOutput>
 </EmbeddedResource>
</ItemGroup>

where a resource file with public modifier looks like this.

<ItemGroup>
 <EmbeddedResource Update="resources.resx">
  <Generator>PublicResXFileCodeGenerator</Generator>
  <LastGenOutput>resources.Designer.cs</LastGenOutput>
 </EmbeddedResource>
</ItemGroup>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael PereiraView Question on Stackoverflow
Solution 1 - C#Mark BonafeView Answer on Stackoverflow
Solution 2 - C#Dave Van den EyndeView Answer on Stackoverflow
Solution 3 - C#Joost SchepelView Answer on Stackoverflow
Solution 4 - C#psychotikView Answer on Stackoverflow
Solution 5 - C#Luis FilipeView Answer on Stackoverflow