This documentation is archived and is not being maintained.

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


Type: System.String
The logical name of the resource.
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.
Type: System.Reflection.ResourceAttributes
The resource attributes.


name has been previously defined.


There is another file in the assembly named fileName.


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


fileName includes a path.


name or fileName is null.


If the file fileName is not found.


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.


Starting with the .NET Framework version 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 version 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!");


	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(

	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. 


	return myAsmBuilder;


   public static void Main() 

	FileStream myResourceFS = null;


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

	AssemblyBuilder myAsm = BuildDynAssembly();

	   myResourceFS = myAsm.GetFile(myResourceFileName);
	catch (NotSupportedException)
	   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.");
	   myResourceFS = new FileStream(myResourceFileName, 
	StreamReader sr = new StreamReader(myResourceFS, System.Text.Encoding.ASCII);



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • ReflectionPermission 

    when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.

  • FileIOPermission 

    The FileIOPermissionAccess.Read permission is needed to access the resource file resourceFileName.

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.