Search code examples
c#.netresx

Why doesn't ResourceWriter generate a valid .resx file?


I am trying to generate a .resx file from a C# program. When I open the generated file, I am getting data like below:

*ÎÊï¾ ‘ lSystem.Resources.ResourceReader, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089#System.Resources.RuntimeResourceSet PADPADP%†ÐÙ Ï C o n t a c t Contact Information**

Here is the code that I am using to write to the file:

string resPath = ConfigurationManager.AppSettings["ResourcePath"];
var wr = new ResourceWriter(resPath);
wr.AddResource("Contact", "Contact Information");
wr.Generate();
wr.Close();

Solution

  • You're using the wrong class. ResourceWriter creates a .resources file, not a .resx file. To create a .resx file, use ResXResourceWriter instead:

    using (ResXResourceWriter writer = new ResXResourceWriter(resPath))
    {
        writer.AddResource(new ResXDataNode("Contact", "Contact Information"));
    }
    

    Note: To use ResXResourceWriter, you must add an assembly reference to System.Windows.Forms.