I've been learning about resources in C# and the visual C# IDE. I'm confused now. I have read some pages on StackOverflow, like this one how-to-get-the-path-of-an-embebbed-resource and the documentation of Microsoft, but it confused me.
My first question: what are resources: is it the .resources file or is it the files that are inside it, like icons.
second: When I use the GetManifestResourceNames method: do I get the .resources files names or the the names of the files inside it. When I use it in my program, I only get the .resources files, but reading topics like this loop-through-all-the-resources-in-a-resx-file , I get the impression I should get the names of the files inside the .resources file.
Is it me, or is this terminology really a bit confusing? Can anyone make it a little clearer? Thanks for all help.
Resources are any file you compile by flagging it as an "EmbeddedResource" this simply merge the file into the assembly. GetManifestResourceNames()
is just an enumerator that give us the name of all embedded compiled resources files, e.g. MyAssembly.resources
. The actual resource elements need to be enumerated via a ResourceSet
which loads this resources file.
I dont know if you still need the answer to this question but from my experience GetManifestResourceNames()
returns the names of the .resource
files embedded in your assembly.
If you want to access the individual resources you could so something along the lines of:
Assembly assembly = Assembly.LoadFrom(assemblyName);
string[] names = assembly.GetManifestResourceNames();
ResourceSet set = new ResourceSet(assembly.GetManifestResourceStream(names[0]));
foreach (DictionaryEntry resource in set)
{
Console.WriteLine("\n[{0}] \t{1}", resource.Key, resource.Value);
}
I got my project to work because of Felice Pollano's answer. I added a folder to my solution called Images, and opened that folder in windows explorer, then copied my image file into the Images folder. Then go into visual studio and click "show all files" at the top of the Solution Explorer, and right-clicked the image file in the Images folder and clicked Include in project. Then i left clicked the image file in the solution explorer, and in the Properties window, set the build action to Embedded Resource like you mentioned.
Here is the code where I accessed this picture
private Dictionary<int, Image> GetImages()
{
List<Stream> picsStrm = new List<Stream>();
Assembly asmb = Assembly.GetExecutingAssembly();
string[] picNames = asmb.GetManifestResourceNames();
foreach (string s in picNames)
{
if (s.EndsWith(".png"))
{
Stream strm = asmb.GetManifestResourceStream(s);
if (strm != null)
{
picsStrm.Add(strm);
}
}
}
Dictionary<int, Image> images = new Dictionary<int, Image>();
int i = 0;
foreach (Stream strm in picsStrm)
{
PngBitmapDecoder decoder = new PngBitmapDecoder(strm,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
BitmapSource bitmap = decoder.Frames[0] as BitmapSource;
Image img = new Image();
img.Source = bitmap;
img.Stretch = Stretch.UniformToFill;
images.Add(i, img);
i++;
strm.Close();
}
return images;
}
Which is actually from this article(A WCF-WPF Chat Application) by Islam ElDemery