Solved

Check to see if resources exist

Posted on 2014-07-17
3
44 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

696 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