Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How do I read *.resx files and create *.resources files in C#?

Locked How do I read *.resx files and create *.resources files in C#?

Locked

  • Wednesday, April 08, 2009 5:52 AM
     
      Has Code

    How do I read *.resx files and create *.resources files in C#?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Wednesday, April 08, 2009 5:52 AM
     
     Answered Has Code

    We can use ResXResourceReader to read *.resx files. 

    Code Snippet:

    // Create a ResXResourceReader for the file items.resx.
    ResXResourceReader rsxr = new ResXResourceReader(@"C:\Resource1.resx");
    
    // Create an IDictionaryEnumerator to iterate through the resources.
    IDictionaryEnumerator id = rsxr.GetEnumerator();
    
    // Iterate through the resources and display the contents
    foreach (DictionaryEntry d in rsxr)
    {
        Console.WriteLine("Key: {0}  Value: {1}",     
                                  d.Key.ToString(),d.Value.ToString());
    }
    

     

    We can use FileStream and ResourceWriter to create a *.resources file. 

    Code Snippet:

    string value = "Hello world";
    string name = "name";
    
    FileStream fs = new FileStream(@"C:\items.resources", FileMode.OpenOrCreate, FileAccess.Write);
    
    IResourceWriter writer = new ResourceWriter(fs);
    writer.AddResource(name, value);
    writer.Close();
    

     

    Related thread:
    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/37bb70b9-5cdb-46dc-bb12-ebeb88061597/


    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

  • Wednesday, April 08, 2009 9:15 AM
     
     Answered Has Code

    Hi,

    You can also use ResourceManager class to access the resources.

    ResourceManager rm;
    this.rm.GetString(resourcename);