Solved

Check to see if resources exist

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 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