简体   繁体   中英

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 .

The file has also been added under the Resources folder and I can change its properties. I set the Build Action to Embedded Resource .

I then try to get the resource by doing:

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

However, stream is null .

I then changed the Build Action to None and Copy to Output Directory to 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)

  • proceed with var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name you got from the debug call )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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