How to use Resources.resx dynamically i,e add new items dynamically

前端 未结 1 1380
無奈伤痛
無奈伤痛 2021-01-21 12:51

Can anyone please tell me how to Add resources (image, string etc) in \'Resources.resx\' dynamically.
I need to add a new bitmap to the Resources.resx from the program at ru

1条回答
  •  失恋的感觉
    2021-01-21 13:19

    You can use ResourceWriter to do just that, but I wouldn't recommend writing dynamic resources to resx files. Instead I would use some kind of database (i.e. embedded one).

    EDIT on invalid answer

    I am sorry, I wasn't precise. My suggestion tells you how to create compiled resource file (the one with .resources extension), not original .resx file. The code below works in terms, that it is able to write image (as System.Drawing) to a file named my.resources. You could surely read one out with ResourceReader:

            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(openFileDialog1.FileName);
                IResourceWriter writer = new ResourceWriter("my.resources");
                writer.AddResource("myImage", bitmap);
                writer.Close();
            }
    

    It seems that you need to do Close() instead of Generate(). I think that by closing resource stream you force it to be written to disk.
    You can make sure that it is there by previewing the file (my.resources, that is) with some hex editor.

    0 讨论(0)
提交回复
热议问题