如何访问一个二进制资源在C#应用程序 [英] How to access a binary resource in a C# application

查看:197
本文介绍了如何访问一个二进制资源在C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储空Access数据库(.mdb)在我的应用程序的资源。然后我想写出来的文件系统,并与表定义填充它,等我在Visual Studio 2005中的问题,我的C#应用​​程序是不能够访问存储在同一个程序集中的资源(我本来以为会是默认访问)。这是我的code:

I'm trying to store an empty Access database (.mdb) as a resource in my application. I then want to write it out the file system and populate it with table definitions, etc. The problem I'm having in Visual Studio 2005 and my C# application is with not being able to access the resource stored in the same assembly (which I would have thought would be accessible by default). Here's my code:

byte[] abytResource;
System.Reflection.Assembly objAssembly = System.Reflection.Assembly.GetExecutingAssembly();
objStream = objAssembly.GetManifestResourceStream("empty.mdb");
abytResource = new Byte[objStream.Length];
objStream.Read(abytResource, 0, (int)objStream.Length);
objFileStream = new FileStream(newDatabasePathWithName, FileMode.Create);
objFileStream.Write(abytResource, 0, (int)objStream.Length);
objFileStream.Close();

GetManifestResourceStream返回NULL,并根据那是因为资源必须是私有的(因为即使它不存在一个非返回NULL值)的文档。所以我的问题是这样的:

GetManifestResourceStream returns NULL and according to the documentation that's because the resource must be private (because even if it doesn't exist a non-NULL value is returned). So my question is this:

如何让我的资源访问自己的应用程序?我已经把它添加到项目中并标记为嵌入的资源,顺便说一句。

How do i make my resource accessible to my own application? I already added it to the project and marked it as "Embedded Resource", by the way.

谢谢!

推荐答案

您需要preFIX的empty.mdb与组件的默认命名空间。是这样的:

You need to prefix the "empty.mdb" with the default namespace of the assembly. Something like:

objStream = objAssembly.GetManifestResourceStream("My.Namespace.empty.mdb");

这篇关于如何访问一个二进制资源在C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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