|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionTime and again questions have come up in the C# and .NET forums relating to the use of embedded resources in VS.NET. For most projects there are no problems, but as you get into more complicated projects this quickly becomes a problem. This article will attempt to put the information needed in a more permanent place, making it easier to find and link to. First things firstThere are a couple different ways resources can be used in VS.NET. The
most common way is how the The second way to use resources, and the one this article will focus on, relies on adding the file to the project and setting the file's "Build Type" property to "Embedded Resource". This is only the first step though! What's in a name?When you embed a resources as I outlined above you will typically need to know its name. This can be as little as the filename, but usually it is more than that. The name is made up of three parts: The first and last parts are easy to identify; the default namespace is set in the project's properties and the filename is...well, the filename of the file. The middle part is where most people get lost. Quickly put, the extended namespace is made up of directory structure of the
project itself. If you place the file in the root of the project, then
there is no extended namespace and the name of the file as an embedded resource
is See the pattern? You can use this for your code as well. When you
create a class in the root of the project the namespace is automatically
generated for you, the namespace follows the same pattern: Getting the resourcesUniversal accessThe standard way to access an embedded resource is to use the The purpose of this demo application will be to list the resources in an assembly, and be able to save selected resources out to a file. An added feature will let us view the contents of a particular resource if it is an image. If you don't have any assemblies with embedded resources handy, the second demo application has one embedded in it which you can use.
The interesting code happens inside of the System.IO.Stream stream = loadedAssembly.GetManifestResourceStream(
(string) resources.SelectedItem);
System.Drawing.Image img = Image.FromStream(stream);
The demo application loads an assembly and stores a reference to the The easiest way to get a reference to the Assembly object for a particular
class is to use the With this technique you can use any resource embedded in an There is another wayThere are actually two overloads of the A few classes in the .NET framework use this pattern as well, but instead of
returning Another one is the The second demo uses the second method to display a bitmap embedded in its own assembly.
Aside from placing the controls on the picture.Image = new Bitmap(typeof(Form1), "demo1.gif");
This tells the See? That wasn't so bad :-) ConclusionOnce you get the hang of it, using embedded resources in your .NET applications is easy. The only hard part is figuring out the name that VS.NET gives to your resources Updates
|
||||||||||||||||||||||||||||||