Changing Resource files (resx) namespace and access modifier

58,209

Solution 1

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

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

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

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

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>
Share:
58,209
Michael Pereira
Author by

Michael Pereira

Updated on January 18, 2020

Comments

  • Michael Pereira
    Michael Pereira over 4 years

    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.

  • Donald Byrd
    Donald Byrd over 14 years
    Very useful blog entry. Thanks!
  • MordechayS
    MordechayS almost 12 years
    This also happens in VS2010: copying + pasting a resx file into the project may have been the issue, it sets the .resx file to GlobalResourceProxyGenerator if you do this.
  • Wagner Leonardi
    Wagner Leonardi about 10 years
    Take care who use ASP.NET, because while publishing with "Embedded Resource" this may not publish at server. "Build Action" as "Content" works fine though
  • Jeff Paquette
    Jeff Paquette almost 10 years
    Simplest answer for VS2012
  • Jacob McKay
    Jacob McKay about 5 years
    Had a .resx file in <repo_name>/Properties directory, and wanted it to be in my main namespace: <repo_name>. It was being updated on save with the namespace of <repo_name>.Properties. Instead of using the <repo_name>.Properties namespace everywhere I was able to use this answer to have the namespace persist at <repo_name> thanks!