Link to home
Start Free TrialLog in
Avatar of loyaliser
loyaliserFlag for United States of America

asked on

reading .resx files

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...
ASKER CERTIFIED SOLUTION
Avatar of naveenkohli
naveenkohli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of loyaliser

ASKER

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...
Avatar of naveenkohli
naveenkohli

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
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...
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
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...
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
interesting... ok, thanks for the help.

that should do it...
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!
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