AddResourceFile Method (String, String)
Collapse the table of content
Expand the table of content

AssemblyBuilder.AddResourceFile Method (String, String)

Adds an existing resource file to this assembly.

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

public void AddResourceFile(
	string name,
	string fileName
)

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.

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-

The length of fileName is zero, or if fileName includes a path.

ArgumentNullException

name or fileName is null.

FileNotFoundException

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.

The managed resources in the file are assumed to be public.

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

NoteNote

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!");

	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, 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.

Community Additions

ADD
Show:
© 2016 Microsoft