How to get the names of all resources in a resource file

As part of the Visual Basic project, I added a resource file (resx) containing a bunch of images.

Now I want to request image names. If I open the resx file in the designer view in the Visual Studio IDE and select the image, the property grid will show me the name property (the default is “file name without extension, but can be changed”).

The background is that I have a mutist that is created at runtime and populated with images from the resource file. To access these images using a key, I have to install it.

My code looks like this (everything is hardcoded):

Dim imagelist as new Imagelist imageList.Images.Add("A", My.Resources.MyImages.A) imageList.Images.Add("B", My.Resources.MyImages.B) imageList.Images.Add("C", My.Resources.MyImages.C) imageList.Images.Add("D", My.Resources.MyImages.D) imageList.Images.Add("E", My.Resources.MyImages.E) .... imageList.Images.Add("XYZ", My.Resources.MyImages.XYZ) 

And I want to achieve this:

 Dim imagelist as new ImageList For Each img in GetMeAllImagesWithNameFromMyResourceFile imageList.Images.Add(img.Name, img.ImageFile) Next 

where Name is a string and ImageFile is System.Drawing.Bitmap

+4
source share
4 answers

See if this piece of code helps.

  Dim runTimeResourceSet As Object Dim dictEntry As DictionaryEntry runTimeResourceSet = My.Resources.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, False, True) For Each dictEntry In runTimeResourceSet If (dictEntry.Value.GetType() Is GetType(Icon)) Then Console.WriteLine(dictEntry.Key) End If Next 

As an example, I used an icon that you will need to change if you use Bitmap.

EDIT: You will need to use the dictEntry.Value link and see how it can be used to add to imagelist.

+8
source

The following is written in C #: you can easily translate this to VB.

 Assembly executingAssembly = GetExecutingAssembly(); foreach (string resourceName in executingAssembly.GetManifestResourceNames()) { Console.WriteLine( resourceName ); } 

Now that you have all the resource names, you can iterate over the list and do something like:

 foreach(string s in executingAssembly.GetManifestResourceNames()) { if (s.EndsWith(".bmp")) { imgStream = a.GetManifestResourceStream(s); if (imgStream != null) { bmp = Bitmap.FromStream(imgStream) as Bitmap; imgStream.Close(); } } } 

I have not tried this, but it should work.

+1
source

Try something like this:

 Dim reader As New ResXResourceReader(resxFilePath) Dim en As IDictionaryEnumerator en = reader.GetEnumerator() While en.MoveNext() Console.WriteLine("Resource Name: [{0}] = {1}", en.Key, en.Value) End While reader.Close() 

You can find more examples that could help you in this link . Examples are written in C #, but it’s not very difficult to change them for vb.net

0
source

While the answers above pointed me in the right direction, I am adding a separate answer to clarify the use of GetResourceSet and the subsequent loading of images:

  Dim resSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.InvariantCulture, True, False) For Each de As DictionaryEntry In resSet If (de.Value.GetType() Is GetType(Bitmap)) Then m_Icons.Add(de.Key, My.Resources.ResourceManager.GetObject(de.Key)) End If Next 

Note the following arguments in My.Resources.ResourceManager.GetResourceSet :

  • using InvariantCulture
  • It takes True to load resources, because at the moment in my class library I have not yet got access to a set of resources, and this forces it to load. This is similar to what @ bcrgen-steinblock meant in his comment, but it was misunderstood in subsequent editing.
  • false is suitable for me because I don't have a set of backup / standard resources
0
source

Source: https://habr.com/ru/post/1286282/


All Articles