[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What's wrong with these 3 lines of code? (C#, .NET)

Posted on 2005-04-18
13
Medium Priority
?
272 Views
Last Modified: 2011-09-20
I am learning how to create a resource file and retrieve info from it. This is what I've done so far:
1. Create a text file ProductNames.txt and use resgen.exe to generate ProductNames.resources. This text file has only two lines:
Shirt = chemise
Hat = chapeau

2. I added ProductNames.resources to WindowsApplication1 project using Add Existing Item

3. Wrote code to display the resource string associated with "Shirt" on a button.
Here's the code:

 private void btnLoad_Click(object sender, System.EventArgs e)
      {
 
         System.String myString;      
         ResourceManager rm = new ResourceManager( "ProductNames", Assembly.GetExecutingAssembly() );

   
         myString = rm.GetString("Shirt");

         btnTestResource.Text = myString;

      }

The following code compiled OK but when I run the program, I got the following error:

"An unhandled exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll

Additional information: Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly.  Make sure "ProductNames.resources" was correctly embedded or linked into assembly "WindowsApplication1"."

How else do I correctly embed the ProductNames.resources to the WindowsApplication1 assembly????  I thought what I did was correct.

Thanks.
0
Comment
Question by:rj94070
  • 6
  • 5
  • 2
13 Comments
 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 540 total points
ID: 13812822
before going into error details, why are you using plain text file to store data. and why not an XML?
Its the best way way to store this kind of data where you don't have to worry about line breaks/string manipulation.
If you haven't given it a thought, please do so.
and if you still want to use text file, let us know and we can work on the error.

Pura
0
 

Author Comment

by:rj94070
ID: 13812849
I did try the XML approach. By using resgen to convert ProductNames.txt to XML format ProductNames.resx. BUT the code samples to access the resource files I've seen all access .resources, so I, again, use resgen to convert the .txt file to .resources.
If you can show me how to correct the code to use XML .resx file, that'll be fine with me.
Thanks.
0
 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 540 total points
ID: 13812894
You don't have to worry about .resx files. Just use the xml where you are storing the data.
here're some samples..
http://download.microsoft.com/download/f/c/5/fc59614c-f610-4ab5-a9bb-82b784741313/CustomApplicationFilesSample.exe
http://download.microsoft.com/download/f/c/5/fc59614c-f610-4ab5-a9bb-82b784741313/XMLTextReaderWriterSample.exe

The second link will be more useful for you.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 540 total points
ID: 13812903
If you've downloaded code from second link..
look at .....XMLTextReaderWriter\Code\XMLReader
That what you need.
0
 

Author Comment

by:rj94070
ID: 13813015
Pura,
I've looked at the code and it's not really what I want.
The reason I have to start with the text file before converting the file to XML or resources or dll or whatever is for ease of translation. It's easier for a translator to deal with a plain text file than with an XML file.

My key question is
why
myString = rm.GetString("Shirt");
didn't work, when I have the string stored in my resource file which is embedded in the assembly.

Thanks.
0
 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 540 total points
ID: 13813044
0
 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 540 total points
ID: 13813093
This is from documentation..
ResourceManager.GetString Method
If no usable set of resources has been found, the ResourceManager falls back on the neutral culture's resources, which are expected to be in the main assembly. If an appropriate culture resource has not been found, a MissingManifestResourceException is thrown.

did you try this?
ResourceManager rm = new ResourceManager( "ProductNames", this.GetType().Assembly);
0
 

Author Comment

by:rj94070
ID: 13813175
I tried
ResourceManager rm = new ResourceManager( "ProductNames", this.GetType().Assembly);

instead of the original code and the same error occured.

I assume the ProductNames.resources is the neutral culture resource. How else do I specify it?

If you have VS .NET installed on your PC, you can try an app just with a few lines of code as I have. It's just so simple and still I can't make it work.
Thanks.
0
 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 540 total points
ID: 13813196
sorry rj. Don't have it installed here.
Have you 'Resgen'ed the file from command prompt as stated in this?
http://www.codeproject.com/dotnet/Localization.asp
0
 

Author Comment

by:rj94070
ID: 13813613
Yes. I did resgen, that's how I got the .resx and the .resources files.
0
 
LVL 11

Accepted Solution

by:
jatinderalagh earned 960 total points
ID: 13813664
  ResourceManager rm = new ResourceManager( "ProductNames", Assembly.GetExecutingAssembly() );
this will be

   ResourceManager rm = new ResourceManager( "DefaultNamespace.ProductNames", Assembly.GetExecutingAssembly() );
0
 
LVL 11

Assisted Solution

by:jatinderalagh
jatinderalagh earned 960 total points
ID: 13813670
DefaultNamespace will be the namespace in which Main method exist
0
 

Author Comment

by:rj94070
ID: 13817196
jatinderalagh,

I think we are getting closer to the solution. Now I no longer get the exception, but the string myString is empty after the GetString().
I will do some further checking.
Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question