如何访问一个二进制资源在C#应用程序应用程序、资源

由网友(有你才叫爱)分享简介:我试图存储空Access数据库(.mdb)在我的应用程序的资源。然后我想写出来的文件系统,并与表定义填充它,等我在Visual Studio 2005中的问题,我的C#应用​​程序是不能够访问存储在同一个程序集中的资源(我本来以为会是默认访问)。这是我的code:I'm trying to store an empt...

我试图存储空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");
阅读全文

相关推荐

最新文章