Browse DevX
Sign up for e-mail newsletters from DevX


How to Embed Resource Files in .NET Assemblies  : Page 2

Use .NET's ability to embed XML files, icons, graphics—anything you want—directly into your assemblies.

Storing Files as Resources in an Assembly
To include a file as a resource inside an assembly, add the file to your project in Visual Studio by dragging it into the Solution Explorer from Windows Explorer, or right-click the project item and select "Add" and then select "Add Existing Item" from the popup menu. After adding the file in your project, select it in the Solution Explorer and then select "Embedded Resource" as the build action in the properties window. (You can also add resources to a managed assembly by using the /resource compiler option if you are compiling your assembly from the command-line).

Select a Build Action
The "Build Action" property tells the compiler what to do with a particular file. Table 1 shows the available options:

EmbedInclude the file as an embedded resource.
CompileCompile the file.
ContentFiles designated as content files can be referenced by Visual Studio setup projects.
NoneDo nothing. Use this action to add documentation or other uncompiled files to your project for reference purposes without embedding them in an assembly.

Accessing Resources at Runtime
To access an embedded resource at runtime, use the GetManifestResourceStream() method of the Assembly class. There are several ways to get an active assembly reference at runtime - some of the common methods are the GetEntryAssembly(), GetExecutingAssembly(), or GetCallingAssembly() methods of the System.Reflection.Assembly class. The following list shows how and when you might want to use each method:

  • GetEntryAssembly: Use this method to reference the executable file that was run to start the current process. The entry assembly is set for Windows forms applications, but for most other applications (for example, web projects), GetEntryAssembly returns 'nothing' (or null, in C#).
  • GetExecutingAssembly: Use this method to reference the same assembly that the executing code is in.
  • GetCallingAssembly: Use this method to reference the assembly containing the code that called the current method. This method is useful if you write generic code to read embedded resources that is inside a shared assembly where the embedded resource is in the calling assembly.
The name argument of the GetManifestResourceStream() method identifies your resource, and should match the resource filename. The argument is case-sensitive, regardless of the .NET language you are using.

If you use a default namespace you must prefix the assembly name to the resource name. For example, to access an icon called "icon1.ico" in a project with a default namespace of "myproject", use the name "myproject.icon1.ico". In both Visual Basic .NET and C#, new projects have a default namespace—Visual Studio uses the project name by default. You can remove or change the default namespace by right-clicking the project item in the Solution Explorer and selecting Properties from the popup menu. The default namespace appears under the Common Properties, General item (see Figure 1). In VB.NET, it's the "root namespace"; in C# it's called the "default namespace".

Figure 1: Use the Visual Studio Project properties dialog to remove or change the default project namespace.
Although you can use folders (directories) to organize files in your project at design-time, at runtime, all manifest resources are in a "flat" structure (no pathnames, just filenames). If you're having trouble identifying your resource name, compile your application and use ildasm.exe to view your assembly manifest—the resources are listed in the form ".mresource public [resource name]".

Thanks for your registration, follow us on our social networks to keep up-to-date