Reputation: 67325
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?
Upvotes: 11
Views: 11125
Reputation: 35318
The current answer does not (at this time) include the fact that it also depends on what sub-folder the resource file is in. The Name
argument on both Assembly.GetManifestResourceStream overloads has the following format
[Assembly Name].[Sub-Folder Path].[File Name]
where:
[Assembly Name]
is the simple name of the assembly, which is usually, but not necessarily, the file name of the manifest file of the assembly, minus its extension. This is the same value returned by Assembly.GetName().Name
[Sub-Folder Path]
is the path of the sub-folder within the Assembly's un-compiled project folder structure where the resource file resides, where each folder level is separated by a period ".".
[File Name]
is the full name of the resource file including the extension.
For example, if you have a project named "ConsoleApp4" (configured by default to compile to an assembly named "ConsoleApp4") and a resource file in the project named "MyResource.txt" which is in a folder named "AnotherSubFolder" which is beneath a parent folder named "SubFolder", like this:
Then you would pass "ConsoleApp4.SubFolder.AnotherSubFolder.MyResource.txt"
as the value for the Name
argument in Assembly.GetManifestResourceStream
.
Don't forget: your resource also needs to be configured as an "embedded resource" for this method call to work properly (i.e. not return null
).
Upvotes: 2
Reputation: 13975
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();
Upvotes: 24