开发者

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

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 runtime. The code i used is

` ResourceWriter rw; rw = new ResourceWriter("myResources.resources");

  rw.AddResource("anImage", new Bitmap("winter开发者_StackOverflow社区.jpg"));

  rw.AddResource("welcomeString", "www.java2s.com");
  rw.Generate();`

But i found nothing in Resources.resx. Kindly tell me how to use ResourceWriter.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜