confused about resources and GetManifestResourceNames()

19,322

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
19,322

Related videos on Youtube

pikachu
Author by

pikachu

pika pika

Updated on June 04, 2022

Comments

  • pikachu
    pikachu almost 2 years

    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.

  • pikachu
    pikachu over 12 years
    OK, I'm think I'm getting closer. I'll try to explain what you said with my project as an example, to be sure I understood. In my project I have two .resx files (one default, and one I added manually (add new item)), meaning two .resources files, right. To the added .resx file I added an icon. So if I get you right, this icon is not a resource? Only the .resources file that contains it?
  • Felice Pollano
    Felice Pollano over 12 years
    @pikachu see the Hans Passant comment.
  • pikachu
    pikachu over 12 years
    I don't see how what Hans Passant says is a clarification of my confusion. I knew that already. I shouldn't have mentioned it in my previous quesion. So, I'm gonna edit it. Update: I can't edit it anymore :), but I guess you understand now.
  • Zack
    Zack about 11 years
    I got my project to work because of this 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.
  • Zack
    Zack about 11 years
    I was then able to retrieve the resource like so 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); } } }
  • Kyle Delaney
    Kyle Delaney over 5 years
    Do resources need .resource extensions? They can't be .txt files etc?
  • Tejas Sharma
    Tejas Sharma over 5 years
    Sorry @KyleDelaney, I wrote this answer quite a while back and don't particularly recall the details of .NET resources / satellite assemblies now as well as I did back then. You might want to ask a new question.