如何使用Resources.resx动态I,E动态添加新项目动态、如何使用、新项目、resx

2023-09-08 08:38:53 作者:⑧洅⑥戀

谁能告诉我怎么加资源(图像,字符串等)在Resources.resx动态。 我需要从程序在运行时一个新的位图添加到Resources.resx。 在code我用的是      ` ResourceWriter RW;       RW =新ResourceWriter(myResources.resources)

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.jpg"));

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

但我发现没有什么Resources.resx。请告诉我怎么用ResourceWriter。

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

推荐答案

您可以使用的 ResourceWriter 做到这一点,但我不建议编写动态资源RESX文件。相反,我会用某种数据库(即嵌入一个)。

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).

修改的答案无效

我很抱歉,我不是precise。我的建议告诉你如何创建编译的资源文件(其中带有的.resources扩展名),而不是原来的.resx文件。下面的code工作而言,这是能写的图像(如System.Drawing中)到指定的文件my.resources。你可以肯定读一出来与ResourceReader:

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();
        }

看来你需要做的关闭(),而不是的生成()。我认为,通过关闭资源流,你强迫它被写入到磁盘。 你可以确保它是有由previewing文件(my.resources,这是)一些十六进制编辑器。

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.