Solved

reading .resx files

Posted on 2002-05-27
10
2,087 Views
Last Modified: 2011-08-23
when creating a .aspx page, VS.NET adds to it a .resx file (e.g. Default.aspx.resx).

i added names/values of error codes in the .resx file - name contains a code number, and values contains error message.

in the codebehind for the Default.aspx page, i use the following to open the file for reading:

ResourceReader objResourceReader = new ResourceReader(strWebRootPath + "Default.aspx.resx");

however, i keep getting this error:

Stream is not a valid resource file!

i have even tried this with .resx files containing no keys/values, and still get the same error.

is it because i should be using ResXResourceReader class?

if so, why can't i instance it in the codebehind?!?! VS.NET compiler complains it does not exist in System.Resources... why?

any way... please advise on how to solve this problem and read from these files the keys/values i have created.

thank you...
0
Comment
Question by:loyaliser
  • 4
  • 4
  • 2
10 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 100 total points
ID: 7037917
loyaliser,
.resx file is not a valid resource file. ResourceReader can read .resources file formats. What you will need to do is convert the .resx file to .resources file and then use the reader to read the value. HEre are the steps.

1. Goto your web folder where you have .resx file.
2. Using command line rool ResGen,
ResGen Default.aspx.resx
3. This will generate Default.aspx.resources file. Take a look at the option for this command line tool to name the file the way you want.
4. Then refer to this recources file in your code. HEre is the example.

private void ReadResources()
{
     try
     {
     IResourceReader reader  = new ResourceReader(Server.MapPath("Default.aspx.resources"));
     IDictionaryEnumerator en = reader.GetEnumerator();

     // Goes through the enumerator, printing out the key and value pairs.
     while (en.MoveNext())
     {
          Response.Write("<br>");
          Response.Write("Name: " + en.Key.ToString());
          Response.Write("Value: "+ en.Value.ToString());
     }
     reader.Close();
     }
catch (Exception ex)
{
Trace.Write(ex.Message);
}
}

Naveen
0
 
LVL 1

Author Comment

by:loyaliser
ID: 7037939
it seems inconvenient to resgen these files each time they change... there is no other way around it?

no .resx class that reads these files?

i guess the only other way is to use Xml namespace to read from file and parse myself... right?

thanks...
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7037974
the resource files are not mere XML files. The are very important when it comes to multi lingual applications. The recommended way of using resource files in an ASP.Net application is by creating Satellite assemblies. These assemblies contain only resx files embedded as resources in the assembly it self. And then depending on the language these satellite assemblies are copied under the web applications's main folder in appropraite sub folders, E.g english resources will be in "en" sub folder, "japanese resoureces will be under "jp" folder. And then based on the culture info of the thread, framework looks for the satellitie assembly in appropriate language sub-folder.
If you developing an aplication where resopurces need to be localized, the I will suggest that you look at the topic of creating Satellite asemblies and deploying them for ASP.NEt applications.
Your RESX file should compile with your existing application too, In the solution explrer, right click on this file and check its type. It should be marked as embedded resource and not as content.

Naveen
0
 
LVL 1

Author Comment

by:loyaliser
ID: 7037989
i checked the .resx file and it is marked as "embedded resource", but its not creating the .resources file automatically.

am i missing something? or misunderstanding what you are saying?

i tried to manually create the .resources file and that's fine... just a pain to do it manually.

thanks...
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7037993
But you can still read the RESX files directly. You will need to use ResXResourceReader instead of ResourceReader which is onlyt for reading resources in binary format like .resources file.

private void ReadResources()
{
     try
     {
          ResXResourceReader reader = new ResXResourceReader(Server.MapPath("Default.aspx.resx"));
          IDictionaryEnumerator en = reader.GetEnumerator();
          while (en.MoveNext())
          {
               Response.Write("<br>");
               Response.Write("Name: " + en.Key.ToString());
               Response.Write("Value: "+ en.Value.ToString());
          }
     }
     catch (Exception ex)
     {
          Trace.Write(ex.Message);
        }
}

Naveen
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:loyaliser
ID: 7038004
i tried ResXResourceReader but the compiler says it does not exist!

how is that possible? it is in the System.Resources namespace isn't it?

this was the problem i had initially...

strange...
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7038085
This method of reading resources is usually used for Windows Forms application. You will need to add reference to System.Windows.Forms assembly. Then you will be able to access it.

Naveen
0
 
LVL 1

Author Comment

by:loyaliser
ID: 7038321
interesting... ok, thanks for the help.

that should do it...
0
 
LVL 1

Expert Comment

by:sae1962at
ID: 36413734
Hello!

Very interesting! Although I use Microsoft .NET 3.5, I cannot find the ResXResourceReader, although I have added the
using System.Resources;

Open in new window

line. Microsoft Visual Studio 2010 Express shows in the error list the message

Error	1	The typr or Namespacename "ResXResourceReader" could not be found. (Is a using directive or an assembly link missing?) 
C:\Documents and Settings\z002zatp\my documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs	154	25	WpfApplication1

Open in new window


And by the way: What is Response in the above code?

Thank you for your information!
0
 
LVL 1

Expert Comment

by:sae1962at
ID: 36413790
I have the code below and get following runtime error, as it the first line in the try-catch block is caught:

---------------------------

---------------------------
MainWindow:LoadCultureInfo: unexpected exceptionSystem.ArgumentException: The Stream is not a valid Resource file.

   at System.Resources.ResourceReader._ReadResources()

   at System.Resources.ResourceReader.ReadResources()

   at System.Resources.ResourceReader..ctor(Stream stream)

   at WpfApplication1.MainWindow.LoadCultureInfo() in C:\Documents and Settings\z002zatp\my documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:Zeile 157.
---------------------------
OK   
---------------------------

Open in new window


But I used these resx files before without having any problems. I think the error message results from a problem in this code. I actually need a FileStream, but a Stream is returned by File.Open. Is this the problem?

Thank you for any help!

CODE:
[...]
                    string FILE_NAME = PATH + "Resource." + aCultureID + ".resx";

                    FileStream cultureFile = File.Open(FILE_NAME, FileMode.Open);

                    try
                    {
                        IResourceReader reader = new ResourceReader(cultureFile); // <<< Exception!

                        IDictionaryEnumerator cultureInfo = reader.GetEnumerator();

                        while (cultureInfo.MoveNext())
                        {
                            MessageBox.Show("<br>");
                            MessageBox.Show("Name: " + cultureInfo.Key.ToString());
                            MessageBox.Show("Value: " + cultureInfo.Value.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(METHOD + "unexpected exception" + ex);
                        throw ex;
                    }
[...]

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now