clamp
clamp

Reputation: 34036

Cant load embedded resource with GetManifestResourceStream()

I am embedding a binary file with the /linkres: compiler argument, but when i try to load it with:

System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
string[] names = myAssembly.GetManifestResourceNames(); // it is really there by its name "shader.tkb"
Stream myStream = myAssembly.GetManifestResourceStream( names[0] );

this leads to a

 Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'shader.tkb' or one of its dependencies. The system cannot find the file specified. ---> System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
--- End of inner exception stack trace ---
at System.Reflection.RuntimeAssembly.GetResource(RuntimeAssembly assembly, String resourceName, UInt64& length, StackCrawlMarkHandle stackMark, Boolean skipSecurityCheck)
at System.Reflection.RuntimeAssembly.GetManifestResourceStream(String name, StackCrawlMark& stackMark, Boolean skipSecurityCheck)
at System.Reflection.RuntimeAssembly.GetManifestResourceStream(String name)

What is the problem here?

Upvotes: 21

Views: 33363

Answers (4)

Suhas Pai
Suhas Pai

Reputation: 45

This link will help you understand how to use embedded resources. The solution given by Aseem works only if the Assembly name is the same as the Default namespace of the project, as mentioned in the 'Application' tab of the project's properties.

What needs to be done (even if Assembly name is not the same as Default namespace of the project) is:

using System.IO;
using System.Reflection;

Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream myStream = assembly.GetManifestResourceStream(fullResourceName);

where fullResourceName is the fully qualified name of the embedded resource you wish to access.

If the resource (here shader.tkb) is in the root folder of the project, then fullResourceName = "Default.Namespace.Of.Project.shader.tkb". If the resource is present inside a folder called Resources located in your project folder, then fullResourceName = "Default.Namespace.Of.Project.Resources.shader.tkb".

Upvotes: 2

Marco Guignard
Marco Guignard

Reputation: 653

You could use GetManifestResourceNames() on your Assembly object to get the whole list of files (and their name).

I don't know why but on my project I shouldn't include the subfolder name. Just the basename of the assembly with the filename.

Upvotes: 2

AORD
AORD

Reputation: 186

Project namespace may need to be taken into account:

What worked for me was:

System.Reflection.Assembly assem = this.GetType().Assembly;         
using( Stream stream = assem.GetManifestResourceStream("Project_A.FolderName.test.txt") )   

Where "Project_A" was my projects namespace.
Where "FolderName" is a folder in my projects Solution Explorer.
Where "test.txt" is an embedded resource in "FolderName" folder.

If i used:

assem.GetName().Name

I would get "Project A" instead of "Project_A" ?!?!?!?

Upvotes: 2

A G
A G

Reputation: 22607

1 - The file's build action should be Embedded Resource.
2 - You can’t just specify the resource name. You have to specify the entire assembly name before the resource name

Assembly assembly = this.GetType().Assembly;
assembly.GetManifestResourceStream(
    assembly.GetName().Name + "." + "SubFolderNameIfAny" + ".shader.tkb");

Upvotes: 41

Related Questions