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.
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.
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.
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
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).
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>
Related
My team has some WPF projects written using XAML. We recently added a large number of local specific RESX files to each project. In order to keep things tidy, I was asked to store these files in [Project]>Localization>[locale]
Now, when we run the application on a non en-US locale, strings are pulled from the appropriate RESX file. We've tried to update Namespaces in the properties of the RESX files, as well as setting them to Public so that a resulting Designer is created.
The only way that we can get things to work is by moving the locale RESX files directly into the Project's Properties directory.
Is there anyway to update the XAML to search for locale RESX files in a subdirectory?
EDIT
XAML Codebase and Solution Explorer
I've attached an image of the problem for clarification.
Some code changes that we've tried:
1) We attempted to set the Namespace for xmlns:res="clr-namespace:[PROJECT].CoreUI.Localization"
2) Attempted to set the Namespace to xmlns:res="clr-namespace:[PROJECT].CoreUI"
3) We've also attempted to change the Custom Tool Namespace of the RESX file to match the current namespace My.Properties as well as CoreUI.Properties
We've also confirmed that the Access Modifiers for both Resources.resx and Resources.ru.resx (for example) are set to Public and that both Build Actions show "Embedded Resource"
Thanks for the help!
My colleague actually stumbled upon another post in here that we somehow missed during out countless searching:
Put translated resx files in a different folder in Visual Studio?
In short, we were thinking about making the project search down into subdirectories for the RESX files. In actuality, we need the RESX file to search for the already created designer further up in the chain.
<ItemGroup>
<EmbeddedResource Include="Localization\ar\Resources.ar.resx" >
<ManifestResourceName>$(TargetName).Properties.%(Filename)</ManifestResourceName>
</EmbeddedResource>
Entering the ManifestResourceName appears to tell the RESX file that we already have a Designer created and not to make a new one.
Thanks again for everyone's help!
You've to first include your folder in the namespace and mention project name like:
xmlns:resx="clr-namespace:Prism_Modules.MyResources;assembly=Prism_Modules"
Also you must include:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Finally, you should call values in resources as:
<dxb:BarButtonItem x:Name="menuFile" Content="{x:Static resx:Resources.mnuFile}" }" />
When moving .resx file to another folder/project, the .Designer.cs file that exposes resource members will still point to the old namespace and also Visual Studio will place it outside the .resx file.
To fix this, I simply deleted the generated .Designer.cs file and then after opening and saving .resx file, the Designer.cs file got generated with proper namespace and I could normally refer to it even from other projects. To generate .Designer.cs file for resource languages/files that have none just open .resx file and use the Access Modifier combobox and VS will create it for you.
I hope I was of any help.
When you create a new C# Windows Forms Application in Visual Studio 2012, It has a Properties folder containing a Resources.resx file.
How do I access this file?
I have tried:
ResourceManager rm=new ResourceManager("Resources.resx",typeof(MyClass).Assembly);
string s=rm.GetString("MyString");
But I get System.Resources.MissingManifestResourceException because for some reason it appends .resources to the filename so it's looking for Resources.resx.resources.
I then appended .resources to the filename to see if it might actually work. It didn't, same exception, why?
I also tried using the ResxResourceReader class but it looks for a resx file in a directory, and this specific resx file I'm trying to access is not stored in a directory, it's compiled in to my assemblies so that doesn't seem to be helping either.
I thought it'd be really simple, maybe it is and I'm just overlooking something?
If it's not simple, I might as well create a C# class and hard-code my strings (only type of resource I need right now).
And another question: If the resx file is compiled in to your exe, does that mean you can't change it's values during runtime?
It is plain simple (usually).
Use the Properties class!
Properties.Resources.MyString
The Properties folder you see in your Solution Explorer is not just a fancy folder ;p Visual Studio generates a class to access all your resources with ease.
Is a class file like MyClass.cs.bak used at run-time even though there is a class file by the name of MyClass.cs within the same Visual Studio project?
Both files are included in the project. The project is of Web Application type, so everything compiles into a single dll when deploying. I am not sure if during compilation, the .bak file is ignored.
I am working on a project in Visual Studio, in which some past developer has included both these files within the project.
If you click on the file in Solution Explorer and look at the Properties window, you'll see a property called "Build Action". This defines whether the file will be treated like code ("Compile"), included as a resource ("Embedded Resource"), or ignored ("None").
When a file is added to a project, the default Build Action is selected based on the file extension. For .bak files, which have no particular meaning to C# projects, the default "None" should be selected, and the file will be ignored when compiling the project.
No.
The .bak file is treated as a normal text file.
This is quite easy to test. Create a new class file, with a class name foo.
Now create a new .cs.bak file and type in the same code.
when you compile the project, you would expect a duplicate class declaration error - this does not occur.
As far as I know (and check), by default, a *.bak file is not considered as a C# class file in a VS Project. It's just another text file which doesn't complied into the assembly as a class module - Therefore, by the way, why you don't get duplicate class names declaration exception.
You can always tell to VS to treat it as a compilable c# file: Properties -> Build Action -> Compile.
It's just look like a backup (bak) source file - just for history purposes, I assume.
We use resx files for globalization, along with database lookups for things that can be configured (such as tab names, which can be different by product) by our CS staff, and thus aren't known at design-time.
I created a custom tool that reads resx files and intelligently dumps the key/value pairs into a relational database (matching values so we don't have duplicates).
This has been a big help to our business - we don't have to send each resx for translation (and pay for duplicate translations of shared words) and we have a 'gold standard' with all our translations (in the database).
The tool I created also reads the database, picking up the key/value pairs and the translations of each value, and creates text files for each resx file (and each language's translation of the text file) and automates running resgen.exe, a command-line tool that ships with Visual Studio, to compile the resx files from the generated text files.
I don't have any source-control integration, so we have to manually check out the resx files and manually check-in the generated files when using the tool, but this hasn't been a big problem.
My problem is that this method is failing for our new MVC projects: the MVC projects require the resx files to be embedded resources with the Access Modifier of 'public'.
Thusfar, we have been fixing this by hand, which introduces the possibility of human error and adds a non-trivial amount of work.
Is there a way to get resgen.exe to create resource files that are embedded and public? If not, is there another way I can create resx files that will do so?
Update, additional question:
The resx files we generate with this method also raise a warning:
A custom tool 'PublicResXFileCodeGenerator' is associated with file '(resxname)',
but the output of the custom tool was not found in the project.
You may try re-running the custom tool by right-clicking on the file in the
Solution Explorer and choosing Run Custom Tool.
The tool mentioned is the tool we initially use to create the resx files. Is there a way I can prevent this warning?
First of, you can generated resources as public by using the /publicClass command line option. Also see: Resgen.exe - Resource File Generator # msdn
Second, I don't think you can let resgen make the resource files embedded resources by default, simply because its not a property of a resource, but a setting of the project.
For example: when you add a new resource "Resource1", using the wizard, a new item group will be added to the project file:
<ItemGroup>
<EmbeddedResource Include="Resource1.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resource1.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
Maybe there are libraries to programmatically modify project files, but not that I know of.
What I would do is just try to serialize and deserialize the project file yourself, and add that section to it, for each resource your generate.
EDIT:
It will also add in a different Item Group:
<ItemGroup>
<Compile Include="Resource1.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resource1.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
</ItemGroup>
So, unless you have a good 3rd Party program to serialize, edit, deserialize the project file. It it probably better to let the wizard do it.
I have struggled with this before but have resourcing working well. This may help although I am not sure. Its always worthwhikle to go over the basics as sometimes its something very simple and .NET resourcing can be infelxible.
I will explain my use of resourcing and hopefully you can apply it to your scenario (I cant really figure out what they exact problem is based on your question.)
Steps to setting up resourcing
1. Add a ASP.NET Folder to your web solution right click on solution select Add > Add ASP.NET Folder > App_GlobalResources
Add a resource file to the folder call it MyResourceFile.resx
Open Properties for file and select
Build Action : Embedded Resource
Custom Tool : PublicResXFileCodeGenerator
Then add your different resource files for different languages etc (specify the same properties for all resource file etc Build Action and Custom Tool)
MyResourceFile.en-GB.resx
MyResourceFile.fr-FR.resx
MyResourceFile.ja-JP.resx
This should auto generate your resource manager which can be accessed through calling
MyResourceFile.MyResourceText
It worth noting that if you havent got a culture installed or its incorrectly defined it wont work and you get all sorts of errors.
Etc MyResourceFile.en-JP.resx would not work (unless you create this custom culture) and cause all sorts of other issues. This culture will be mapped from the CultureInfo in the application to determine which resource file to use, therefore it must be a valid CultureInfo.
We did end up finding a solution to this problem.
For MVC projects, we changed the tool to use the ResXResourceWriter class to update the resx files, matching existing keys and adding new ones as needed.
This preserves the 'Embedded Resource' status as well as all of the other details that are needed.
We still have to set the file up correctly the first time it's created, but that is a much more manageable problem.
I have an image that is used in some PDF files that my C# application generates. I know how to reference the image file when it is located in my workspace, but when I compile the program, I don't see the image anywhere in the compiled directory.
Can someone tell me what happened to that file, or do I have to manually package the file along with my program when I send the program to the users? I added the image to the workspace by drag-drop to the resource directory of one of my namespaces.
Check the file's properties in Visual Studio. Have you set the CopyToOutputDirectory property to something besides Do not copy?
Hmm... I'm not sure about the whole drag-drop business, but if it's all working the resource will have been embedded into your assembly.
I suggest you have a look with Reflector - you can see the resources embedded within assemblies using that.
Have a look at the build properties for the item in Solution Explorer - in particular, if the build action is Embedded Resource then it will indeed be built into the assembly.