1

I found this example of how to read from .resx file

    public static void ReadResourceFile()
    {
        ResourceManager rm = new ResourceManager("XmlReadAndWritePractice.Resource1", Assembly.GetExecutingAssembly());
        //String strWebsite = rm.GetString("Website",CultureInfo.CurrentCulture);   
        String strName = rm.GetString("FirstName");
        Console.WriteLine(strName);

    }

But how do I edit or add to .resx file? I found the following example but it's not what I'm expecting.

    private static void CreateResourceFile()
    {
        // Creates a resource writer.
        IResourceWriter writer = new ResourceWriter("c:\\temp\\Resource1.resx");

        // Adds resources to the resource writer.
        writer.AddResource("String 1", "First String");

        writer.AddResource("String 2", "Second String");

        writer.AddResource("String 3", "Third String");

        // Writes the resources to the file or stream, and closes it.
        writer.Close();

    }

enter image description here

Resource References

ResourceManager

ResourceWriter

Rod
  • 14,529
  • 31
  • 118
  • 230
  • https://stackoverflow.com/questions/36051558/c-sharp-how-do-i-write-to-a-resx-file should help – Saad A Oct 18 '17 at 19:57

3 Answers3

2

This happens because you are generating the binary of the resx file.

Have you tried this?

https://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx

1

You need to call generate before the close.

writer.Generate();
writer.Close();
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • `Close` already saves resources - so no need to call `Generate`, but more importantly this suggestion not even tries to answer OP's problem with getting binary version of resource (.resource format) when they looking for text output(.resx). – Alexei Levenkov Mar 17 '16 at 04:56
1
private static void CreateResourceFile()
{
    using (ResXResourceWriter writer = new ResXResourceWriter("c:\\temp\\Resource1.resx"))
    {
        writer.AddResource("String 1", "First String");
        writer.AddResource("String 2", "Second String");
        writer.AddResource("String 3", "Third String");
        writer.Generate();
    }
}
nobody
  • 10,892
  • 8
  • 45
  • 63
  • So apparently my issue was I was doing this in a Console application. As soon as I tried with Windows Forms application it worked perfect. Any insights? – Rod Mar 18 '16 at 02:36
  • It could have something to do with ResXResourceWriter being part of the winforms assembly. You can get them packaged separately with [ResXResourceReader.NetStandard](https://www.nuget.org/packages/ResXResourceReader.NetStandard) – farlee2121 Mar 03 '20 at 00:55