Cant load embedded resource with GetManifestResourceStream()

天涯浪子 提交于 2019-12-10 00:49:55

问题


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?


回答1:


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");



回答2:


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" ?!?!?!?




回答3:


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.




回答4:


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".



来源:https://stackoverflow.com/questions/16731991/cant-load-embedded-resource-with-getmanifestresourcestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!