AssemblyBuilder.AddResourceFile Method (String, String, ResourceAttributes)

Adds an existing resource file to this assembly.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public void AddResourceFile(
	string name,
	string fileName,
	ResourceAttributes attribute
)

Parameters

name
Type: System.String

The logical name of the resource.

fileName
Type: System.String

The physical file name (.resources file) to which the logical name is mapped. This should not include a path; the file must be in the same directory as the assembly to which it is added.

attribute
Type: System.Reflection.ResourceAttributes

The resource attributes.

ExceptionCondition
ArgumentException

name has been previously defined.

-or-

There is another file in the assembly named fileName.

-or-

The length of name is zero or if the length of fileName is zero.

-or-

fileName includes a path.

ArgumentNullException

name or fileName is null.

FileNotFoundException

If the file fileName is not found.

SecurityException

The caller does not have the required permission.

fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.

Attributes can be specified for the managed resource.

The specified resource file must be in the directory where the assembly will be saved.

NoteNote

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

The following code sample demonstrates how to attach a resource file to a dynamically created assembly, using AddResourceFile.


using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class AsmBuilderGetFileDemo

{
   private static string myResourceFileName = "MyResource.txt";

   private static FileInfo CreateResourceFile() 
   {

     	FileInfo f = new FileInfo(myResourceFileName); 
	StreamWriter sw = f.CreateText();

	sw.WriteLine("Hello, world!");

	sw.Close();

	return f;

   }

   private static AssemblyBuilder BuildDynAssembly()
   {

	string myAsmFileName = "MyAsm.dll";
	
	AppDomain myDomain = Thread.GetDomain();
	AssemblyName myAsmName = new AssemblyName();
	myAsmName.Name = "MyDynamicAssembly";	

	AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
						myAsmName,
						AssemblyBuilderAccess.RunAndSave);

	myAsmBuilder.AddResourceFile("MyResource", myResourceFileName);

	// To confirm that the resource file has been added to the manifest, 
	// we will save the assembly as MyAsm.dll. You can view the manifest 
	// and confirm the presence of the resource file by running  
	// "ildasm MyAsm.dll" from the prompt in the directory where you executed
	// the compiled code. 

	myAsmBuilder.Save(myAsmFileName);	

	return myAsmBuilder;

   }

   public static void Main() 
   {

	FileStream myResourceFS = null;

	CreateResourceFile();

	Console.WriteLine("The contents of MyResource.txt, via GetFile:");

	AssemblyBuilder myAsm = BuildDynAssembly();

	try 
        {
	   myResourceFS = myAsm.GetFile(myResourceFileName);
        }
	catch (NotSupportedException)
	{
	   Console.WriteLine("---");
	   Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile\nis not supported " +
			     "in this SDK build.");
	   Console.WriteLine("The file data will now be retrieved directly, via a new FileStream.");
	   Console.WriteLine("---");
	   myResourceFS = new FileStream(myResourceFileName, 
					 FileMode.Open);
	}
	 
	StreamReader sr = new StreamReader(myResourceFS, System.Text.Encoding.ASCII);
	Console.WriteLine(sr.ReadToEnd());
	sr.Close();

   }

}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft