Recipe13.14.Reading Manifest Resources Programmatically


Recipe 13.14. Reading Manifest Resources Programmatically

Problem

You need to obtain information about manifest resources.

Solution

Use the GetManifestResourceInfo and GetManifestResourceNames methods of the Assembly class:

 public static void DisplayManifestResourceInfo(string asmPath) {     Assembly asm = Assembly.LoadFrom(asmPath);     foreach (string resName in asm.GetManifestResourceNames())     {         Console.WriteLine("Resource Name: " + resName);         ManifestResourceInfo mri = asm.GetManifestResourceInfo(resName);         Console.WriteLine("\rFileName: " + mri.FileName);         Console.WriteLine("\rResourceLocation: " + mri.ResourceLocation);         if (mri.ReferencedAssembly != null)             Console.WriteLine("\rReferencedAssembly: " +                                mri.ReferencedAssembly.FullName);     } } 

Discussion

To obtain a ManifestResourceInfo object or objects, you must first call the GetManifestResourceNames method on an Assembly object. This method returns an array of string objects, which contain the resource name. This resource name is then passed in to the GetManifestResourceInfo method to obtain the ManifestResourceInfo object. The ManifestResourceInfo object is what contains all the information about the manifest resource.

The ManifestResourceInfo.FileName property returns a fully qualified string describing the file that contains the manifest resource. This property will return an empty string if the resource is embedded in the same assembly on which the GetManifestResourceInfo was called.

The ManifestResourceInfo.ResourceLocation property returns all the ResourceLocation flags for this resource. These flags can be one or a combination of three values described in Table 13-4.

Table 13-4. ResourceLocation enumeration flags

Enumeration value

Definition

ContainedInAnotherAssembly

This resource exists in a separate assembly.

ContainedInManifestFile

This resource exists in the manifest file.

Embedded

This resource is an embedded resource, as opposed to a linked resource from a separate assembly.


The final property of use in the ManifestResourceInfo class is the ReferencedAssembly property. This property returns an Assembly object that contains the resource. This property returns null when the resource is embedded in the assembly on which this property was called.

See Also

See the "GetManifestResourceInfo Method" and "GetManifestResourceNames Method" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net