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

  • Question

  • 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.
    Wednesday, April 8, 2009 5:52 AM

Answers

  • 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 8, 2009 5:52 AM
  • Hi,

    You can also use ResourceManager class to access the resources.

    ResourceManager rm;
    this.rm.GetString(resourcename);
    
    Wednesday, April 8, 2009 9:15 AM

All replies

  • 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 8, 2009 5:52 AM
  • Hi,

    You can also use ResourceManager class to access the resources.

    ResourceManager rm;
    this.rm.GetString(resourcename);
    
    Wednesday, April 8, 2009 9:15 AM