Solved

Check to see if resources exist

Posted on 2014-07-17
3
37 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

743 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

14 Experts available now in Live!

Get 1:1 Help Now