C# 无法使用 GetManifestResourceStream() 加载嵌入资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16731991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant load embedded resource with GetManifestResourceStream()
提问by clamp
I am embedding a binary file with the /linkres: compiler argument, but when i try to load it with:
我正在使用 /linkres: 编译器参数嵌入一个二进制文件,但是当我尝试加载它时:
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?
这里有什么问题?
采纳答案by Aseem Gautam
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
1 - 文件的构建操作应该是嵌入式资源。
2 - 您不能只指定资源名称。您必须在资源名称之前指定整个程序集名称
Assembly assembly = this.GetType().Assembly;
assembly.GetManifestResourceStream(
assembly.GetName().Name + "." + "SubFolderNameIfAny" + ".shader.tkb");
回答by AORD
Project namespacemay 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 folderin my projects Solution Explorer.
Where "test.txt" is an embedded resourcein "FolderName" folder.
其中“Project_A”是我的项目命名空间。
其中“FolderName”是我的项目解决方案资源管理器中的一个文件夹。
其中“test.txt”是“FolderName”文件夹中的嵌入资源。
If i used:
如果我使用:
assem.GetName().Name
I would get "Project A" instead of "Project_A" ?!?!?!?
我会得到“项目 A”而不是“项目_A”?!?!?!?
回答by Marco Guignard
You could use GetManifestResourceNames() on your Assembly object to get the whole list of files (and their name).
您可以在 Assembly 对象上使用 GetManifestResourceNames() 来获取整个文件列表(及其名称)。
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.
我不知道为什么,但在我的项目中,我不应该包含子文件夹名称。只是带有文件名的程序集的基本名称。
回答by Suhas Pai
This linkwill 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.
此链接将帮助您了解如何使用嵌入式资源。仅当程序集名称与项目的默认命名空间相同时,Aseem 给出的解决方案才有效,如项目属性的“应用程序”选项卡中所述。
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 fullResourceNameis the fully qualified name of the embedded resource you wish to access.
fullResourceName您希望访问的嵌入资源的完全限定名称在哪里。
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 Resourceslocated in your project folder, then fullResourceName = "Default.Namespace.Of.Project.Resources.shader.tkb".
如果资源(此处shader.tkb)位于项目的根文件夹中,则fullResourceName = "Default.Namespace.Of.Project.shader.tkb". 如果资源Resources位于项目文件夹中名为的文件夹中,则fullResourceName = "Default.Namespace.Of.Project.Resources.shader.tkb".

