Solved

Check to see if resources exist

Posted on 2014-07-17
3
41 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

803 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