简体   繁体   English

文件作为未嵌入的资源

[英]File as a resource not being embedded

I added a binary file as a resource in my assembly (class library).我在我的程序集(类库)中添加了一个二进制文件作为资源。 The Persistence property is disabled and it's set to Linked at compile time . Persistence属性被禁用,它被设置为Linked at compile time

The file has also been added under the Resources folder and I can change its properties.该文件也已添加到Resources folder ,我可以更改其属性。 I set the Build Action to Embedded Resource .我将Build Action设置为Embedded Resource

I then try to get the resource by doing:然后我尝试通过执行以下操作来获取资源:

var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myfile.bin")

However, stream is null .但是,流为null

I then changed the Build Action to None and Copy to Output Directory to Copy always .然后我将Build Action更改为None并将Copy to Output Directory更改为Copy always On build the file is not copied.在构建时不复制文件。

What I actually need is to have the file embedded in the assembly and accessed as shown above.我真正需要的是将文件嵌入到程序集中并如上所示访问。

What am I missing?我错过了什么?

  • Add your file to the project (not as anything special. Just select "Add existing item" and pick your file.)将您的文件添加到项目中(没什么特别的。只需选择“添加现有项目”并选择您的文件。)

  • Pick "Embedded Resource" as the build action of that file.选择“嵌入式资源”作为该文件的构建操作。

  • Call assembly.GetManifestResourceNames() for debugging once to find out the name that you need to use (it contains the namespace and some more stuff)调用 assembly.GetManifestResourceNames() 进行一次调试以找出您需要使用的名称(它包含命名空间和其他一些内容)

  • proceed with var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name you got from the debug call )继续var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(从调试调用中获得的名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM