Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading .resx files

Posted on 2002-05-27
10
Medium Priority
?
2,259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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:Ahmet Ekrem SABAN
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:Ahmet Ekrem SABAN
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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