Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2297
  • Last Modified:

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...
0
loyaliser
Asked:
loyaliser
  • 4
  • 4
  • 2
1 Solution
 
naveenkohliCommented:
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
 
loyaliserAuthor Commented:
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
 
naveenkohliCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

that should do it...
0
 
Ahmet Ekrem SABANSenior IT consultantCommented:
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
 
Ahmet Ekrem SABANSenior IT consultantCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now