VB icon

Get Embedded Resource

Email
Submitted on: 1/1/2015 6:22:00 PM
By: Jon Davis (from psc cd)  
Level: Intermediate
User Rating: By 6 Users
Compatibility: C#
Views: 1671
 
     If you want to drop a text file, an XML file, an icon, or a bitmap, or any other file, into your project directory and have it embedded with your solution, this will make it much easier to retrieve these resources. For instance, if you drop a file called "MainMenu.xml" into a project subdirectory called "Resources" (or, "[project directory]\Resources\MainMenu.xml") and you've marked it as an Embedded Resource, you can retrieve it programmatically like so: XmlDocument mainMenu = (XmlDocument)App.GetEmbeddedResource("Resources/MainMenu.xml");
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Get Embedded Resource
// Description:If you want to drop a text file, an XML file, an icon, or a bitmap, or any other file, into your project directory and have it embedded with your solution, this will make it much easier to retrieve these resources. For instance, if you drop a file called "MainMenu.xml" into a project subdirectory called "Resources" (or, "[project directory]\Resources\MainMenu.xml") and you've marked it as an Embedded Resource, you can retrieve it programmatically like so: XmlDocument mainMenu = (XmlDocument)App.GetEmbeddedResource("Resources/MainMenu.xml");
// By: Jon Davis (from psc cd)
//**************************************

//
// This code is translated to VB.Net at
// http://www.jondavis.net/files/misc/GetEmbeddedResource_VB.txt
//
using System;
// TODO: Set the following line.
namespace MyProjectNamespace {
public class App
{
	public static System.Reflection.Assembly Assembly {
		get {
			return ClassType.Assembly;
		}
	}
	public static Type ClassType {
		get {
			return typeof(App);
		}
	}
	/// <summary>
	/// The type of object is determined by the filename extension.
	/// For example, ".txt", ".htm[l]" are strings, ".xml" is an 
	/// XmlDocument, ".ico" is an Icon, ".bmp", ".gif", ".jpg", 
	/// ".tif", and ".png" are Bitmaps. Anything else is just a 
	/// stream.
	/// </summary>
	/// <param name="resName">The filename of the embedded resource.</param>
	/// <returns>If recognized, a String, XmlDocument, Icon, or Bitmap. 
	/// Otherwise, a Stream object.</returns>
	public static object GetEmbeddedResource(string resName) {
		resName = resName.Replace("/", ".").Replace("\\", ".");
		if (resName.IndexOf(App.ClassType.Namespace) != 0) {
			resName = App.ClassType.Namespace + "." + resName;
		}
		Stream s = App.Assembly.GetManifestResourceStream(resName);
		if (s != null) {
			string ext = resName.Substring(resName.LastIndexOf(".") + 1);
			switch (ext.ToLower()) {
				case "txt":
				case "htm":
				case "html":
					StreamReader sr = new StreamReader(s);
					return sr.ReadToEnd();
				case "xml":
				case "config":
					StreamReader sr2 = new StreamReader(s);
					XmlDocument xDoc = new XmlDocument();
					xDoc.LoadXml(sr2.ReadToEnd());
					return xDoc;
				case "ico":
					return new System.Drawing.Icon(s);
				case "bmp":
				case "gif":
				case "jpg":
				case "jpeg":
				case "exif":
				case "wmf":
				case "emf":
				case "png":
				case "tif":
				case "tiff":
					return new System.Drawing.Bitmap(s);
				default:
					return s;
			}
		}
		return null;
	}
}
}


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.