Solved

Check to see if resources exist

Posted on 2014-07-17
3
40 Views
Last Modified: 2016-06-21
I have several images loaded into the resource list.   Is there a way to see if a specific filename is in the list?
0
Comment
Question by:Tom_Hickerson
3 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40203434
You can enumerate all the resources in the assembly using this code:
using System.Reflection;
...
// Get this type's assembly
Assembly assem = this.GetType().Assembly;

// Enumerate the assembly's manifest resources
foreach( string resourceName in assem.GetManifestResourceNames() ) {
  MessageBox.Show(resourceName);
}

Open in new window

Also, you can load a resource by name:
using System.IO;

public Form1() {
  ...

  // Get this type's assembly
  Assembly assem = this.GetType().Assembly;

  // Get the stream that holds the resource
  // NOTE1: Make sure not to close this stream!
  // NOTE2: Also be very careful to match the case
  //        on the resource name itself
  Stream stream =
    assem.GetManifestResourceStream("Azul.jpg");

  // Load the bitmap from the stream
  this.BackgroundImage = new Bitmap(stream);
}

Open in new window

Check this nice article for more information.
BTW, you can check if the specific resource exists using GetManifestResourceInfo method. If the resource is not found the method will return null. Something like this:
  // Get this type's assembly
  Assembly assem = this.GetType().Assembly;

  ManifestResourceInfo info =
    assem.GetManifestResourceInfo("Azul.jpg");
  if(!info) // resource not found
     MessageBox.Show("resource not found");

Open in new window

0
 
LVL 1

Author Comment

by:Tom_Hickerson
ID: 40204414
Thanks for the info.    I am using visual studio 2012 and this is a visual basic app.  Do you know the syntax for vb?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now