Solved

reading .resx files

Posted on 2002-05-27
10
2,137 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Classic ASP - SQL Server connection problem 5 68
Help with stored procedure 30 66
INNER JOIN ? 8 47
CDO.Message not able to send attachement 5 26
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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/…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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