Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create resource file programmatically

Tags:

c#

resources

I'd like to create a resource file (in the process of preparing for deployment), filling it with certain settings (big XML structure) and some texts but I'm not sure how to go about doing that.

I did find some examples using ResXResourceWriter but when trying to open it cannot find the resource-key. Here's what I have so far :

private void simpleButton1_Click(object sender, EventArgs e)
{
    using (System.IO.MemoryStream oStream = new System.IO.MemoryStream())
    {
        this.layoutControl1.SaveLayoutToStream(oStream);
        using (ResXResourceWriter oWriter = new ResXResourceWriter(@"..\..\Properties\LayoutControl.resources.Resx"))
        {
            oWriter.AddResource("one", oStream.GetBuffer());
            oWriter.Generate();
            oWriter.Close();
        }
    }

}

private void simpleButton2_Click(object sender, EventArgs e)
{
    ResourceManager rm = new ResourceManager("WindowsFormsApplication1.LayoutControl", Assembly.GetExecutingAssembly());
    var one = rm.GetObject("one");
    Console.WriteLine("");
}

I create the resource by clicking simpleButton1, I then stop the app, do an add-existing-item into my project, recompile and click simpleButton2, then I get a

MissingManifestResourceException (Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "WindowsFormsApplication1.LayoutControl.resources" was correctly embedded or linked into assembly "WindowsFormsApplication1" at compile time, or that all the satellite assemblies required are loadable and fully signed.)

Can someone give me some pointers or better yet, a working example? And I would prefer it if the resource would compile into a 'seperate' assembly like 'normal' resource files.

like image 406
Jurjen Avatar asked Dec 12 '09 22:12

Jurjen


1 Answers

You're pretty close - the baseName you're passing in to the ResourceManager is a bit off.

Any resx files you compile in get named with the default namespace plus any folders in the path. In your post, you should be passing in "WindowsFormsApplication1.Properties.LayoutControl.resources".

like image 189
djeebus Avatar answered Oct 08 '22 11:10

djeebus