从内容资源文件中读取 [英] Read from content resource file

查看:92
本文介绍了从内容资源文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资源文件中添加了一个图像并读取了它.很简单.
但是,当我将构建操作更改为内容,然后尝试读取它时,给了我例外.

I added an image in resource file and read it. It was easy.
But when i changed the build action to content and then tried to read it, gave me exception.

var rm = new ResourceManager("Resource1", System.Reflection.Assembly.GetExecutingAssembly());
pictureBox1.Image = (Bitmap)rm.GetObject("excel");





does any one has an idea on this.

推荐答案

通常,在实践中,这要容易得多.您通常不需要编写任何代码.

让我们来看看您的位图.创建一个新的.resx文件,将其打开,转到顶部菜单,然后单击资源" ...添加现有文件".浏览并加载位图文件,说"MyPicture.png".它将在您的项目中添加"MyPicture.png"的副本,并在您的资源中指向此文件的链接.

您的资源将在解决方案资源管理器中创建两个节点:.resx文件及其带有自动生成的.cs文件的子节点.打开此文件,找到一个静态类及其静态属性,其名称与您的位图文件的名称接近,例如"MyPicture".这是您的位图,这是一个静态属性,您应该使用它来访问资源中的位图.

使用它!
不要使用ResourceManager或类似的东西-已经为您完成了!



关于讨论:

如果需要读取几乎永久的文件(例如资源,在运行时未修改的文件),请将其放置在可执行文件目录或相对于可执行文件目录的相对路径中.计算方法如下:

Typically, in practice, this is much more easy than that. You usually don''t need to write any code.

Let''s take your bitmap. Create an new .resx file, open it, go to the menu on top and click "Resources"... "Add existing file". Browse and load the bitmap file, say "MyPicture.png". It will add a copy of "MyPicture.png" to your project and a link to this file in your resource.

You resource will create two nodes in solution explorer: .resx file and its child node with auto-generated .cs file. Open this file, locate a static class and its static property with the name close to the name of your bitmap file, something like "MyPicture". This is your bitmap, a static property you should use to access your bitmap in your resource.

Use it!
Don''t use ResourceManager or anything like that — it is already done for you!



In connection to discussion:

If you need to read a nearly permanent file (like a resource, something not modified during run time), place it in executable directory or a relative path relative to the executable directory. Here is how to calculate it:

string exePath =
    System.IO.Path.GetDirectoryName(
       System.Reflection.Assembly.GetEntryAssembly().Location);



—SA



—SA


用于构建操作的文件设置如下所示的内容将不包含在程序集中.结果,当您尝试使用资源管理器加载文件时,会收到错误消息.您可能想查看msdn以获得有关构建操作的更多详细信息.
A file setup for build action Content as shown below will not be included in the assembly. As a result whe you try to load the file using the resource manager you get an error. You might want to check out msdn for more details on build actions.


我尝试将其读取为xml,但确实起作用了

I tried reading it as xml and it did worked

var x = new XmlDocument();
               XmlNode rootNode;
               x.Load("C:\\Users\\njain\\Documents\\Visual Studio 2008\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\Resource1.resx");
               rootNode = x.SelectSingleNode("root");
               foreach (XmlNode Node in rootNode)
               {
                   if ((Node.Name == "data") && Node.Attributes["name"].Value == "excel")
                   {
                       var cc = Node.LastChild.LastChild.Value;
                       var c=cc.Split(';');
                   }
               }


这篇关于从内容资源文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆