Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 554
clamp Avatar asked May 24 '13 09:05

clamp


People also ask

What is GetManifestResourceStream?

GetManifestResourceStream(Type, String) Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly. GetManifestResourceStream(String) Loads the specified manifest resource from this assembly.

How do I add embedded resources to Visual Studio?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.

Why use embedded resource c#?

Embedded files and their Advantages Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System. Reflection namespace. Any file within the project can be made into an embedded file.


1 Answers

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");
like image 93
Aseem Gautam Avatar answered Sep 27 '22 20:09

Aseem Gautam