Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

c# - What is the Resource Name for a Manifest Resource (Assembly.GetManifestResourceStream)?

I've embedded a resource (an XSD file) in my ASP.NET MVC application. Now, I'd like to retrieve this file from code.

Looking at the documentation for Assembly.GetManifestResourceStream(), it takes a string argument that is the "case-sensitive name of the manifest resource being requested".

Well, I don't find that very helpful and, of course, there was no example to be found on MSDN. What is the name of the manifest resource? Is it the name of the file? Is it the full path and name of the file? Or is it a separate name that is given to the resource itself and, if so, how do I set that name?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I remember correctly, it's the name of the assembly plus the full name of the file (extension included), with a dot between them.

A quick way to find out for sure: use the Assembly.GetManifestResourceNames method:

string[] result = myAssembly.GetManifestResourceNames();

... and inspect the contents of result in the debugger.

ETA: Heck, if you only have one resource, you could just use this code to get the name:

string name = myAssembly.GetManifestResourceNames()[0];

I don't recommend that, though: I've found that sometimes VS will create empty resource files and compile them into the assembly, so you might have more assembly resources than you think.

ETA 2018-06-26: Here's another way I'm surprised I didn't think of in 2013. This will give you the resource name based on the name of the original resource:

string resourceFileName = "myResource.xsd";
string resourceName = myAssembly.GetManifestResourceNames()
                                .Where(name => name.Contains(resourceFileName))
                                .FirstOrDefault();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...