AssemblyBuilder.AddResourceFile AssemblyBuilder.AddResourceFile AssemblyBuilder.AddResourceFile AssemblyBuilder.AddResourceFile Method

Definición

Agrega un archivo de recursos existente a este ensamblado.Adds an existing resource file to this assembly.

Sobrecargas

AddResourceFile(String, String) AddResourceFile(String, String) AddResourceFile(String, String) AddResourceFile(String, String)

Agrega un archivo de recursos existente a este ensamblado.Adds an existing resource file to this assembly.

AddResourceFile(String, String, ResourceAttributes) AddResourceFile(String, String, ResourceAttributes) AddResourceFile(String, String, ResourceAttributes) AddResourceFile(String, String, ResourceAttributes)

Agrega un archivo de recursos existente a este ensamblado.Adds an existing resource file to this assembly.

AddResourceFile(String, String) AddResourceFile(String, String) AddResourceFile(String, String) AddResourceFile(String, String)

Agrega un archivo de recursos existente a este ensamblado.Adds an existing resource file to this assembly.

public:
 void AddResourceFile(System::String ^ name, System::String ^ fileName);
public void AddResourceFile (string name, string fileName);
member this.AddResourceFile : string * string -> unit
Public Sub AddResourceFile (name As String, fileName As String)

Parámetros

name
String String String String

Nombre lógico del recurso.The logical name of the resource.

fileName
String String String String

Nombre de archivo físico (archivo .resources) al que se asigna el nombre lógico.The physical file name (.resources file) to which the logical name is mapped. Este no debe incluir una ruta de acceso; el archivo debe estar en el mismo directorio que el ensamblado al que se agrega.This should not include a path; the file must be in the same directory as the assembly to which it is added.

Excepciones

Se ha definido previamente name.name has been previously defined.

O bien-or- Hay otro archivo en el ensamblado denominado fileName.There is another file in the assembly named fileName.

O bien-or- La longitud de name es cero.The length of name is zero.

O bien-or- La longitud de fileName es cero, o si fileName incluye una ruta de acceso.The length of fileName is zero, or if fileName includes a path.

No se encuentra el archivo fileName.The file fileName is not found.

El llamador no dispone del permiso requerido.The caller does not have the required permission.

Ejemplos

Ejemplo de código siguiente muestra cómo asociar un archivo de recursos a un ensamblado creado dinámicamente, utiliza AddResourceFile.The following code sample demonstrates how to attach a resource file to a dynamically created assembly, using AddResourceFile.

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class AsmBuilderGetFileDemo
{
public:
   static String^ myResourceFileName = "MyResource.txt";
   static FileInfo^ CreateResourceFile()
   {
      FileInfo^ f = gcnew FileInfo( myResourceFileName );
      StreamWriter^ sw = f->CreateText();
      sw->WriteLine( "Hello, world!" );
      sw->Close();
      return f;
   }

   static AssemblyBuilder^ BuildDynAssembly()
   {
      String^ myAsmFileName = "MyAsm.dll";
      AppDomain^ myDomain = Thread::GetDomain();
      AssemblyName^ myAsmName = gcnew 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;
   }

};

int main()
{
   FileStream^ myResourceFS = nullptr;
   AsmBuilderGetFileDemo::CreateResourceFile();
   Console::WriteLine( "The contents of MyResource.txt, via GetFile:" );
   AssemblyBuilder^ myAsm = AsmBuilderGetFileDemo::BuildDynAssembly();
   try
   {
      myResourceFS = myAsm->GetFile( AsmBuilderGetFileDemo::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 = gcnew FileStream( AsmBuilderGetFileDemo::myResourceFileName,FileMode::Open );
   }

   StreamReader^ sr = gcnew StreamReader( myResourceFS,System::Text::Encoding::ASCII );
   Console::WriteLine( sr->ReadToEnd() );
   sr->Close();
}


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();

   }

}


Imports System
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class AsmBuilderGetFileDemo
   
   Private Shared myResourceFileName As String = "MyResource.txt"
   
   
   Private Shared Function CreateResourceFile() As FileInfo
      
      Dim f As New FileInfo(myResourceFileName)
      Dim sw As StreamWriter = f.CreateText()
      
      sw.WriteLine("Hello, world!")
      
      sw.Close()
      
      Return f

   End Function 'CreateResourceFile
    
   
   Private Shared Function BuildDynAssembly() As AssemblyBuilder
      
      Dim myAsmFileName As String = "MyAsm.dll"
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder = 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

   End Function 'BuildDynAssembly
    
   
   Public Shared Sub Main()
      
      Dim myResourceFS As FileStream = Nothing
      
      CreateResourceFile()
      
      Console.WriteLine("The contents of MyResource.txt, via GetFile:")
      
      Dim myAsm As AssemblyBuilder = BuildDynAssembly()
      
      Try

         myResourceFS = myAsm.GetFile(myResourceFileName)

      Catch nsException As NotSupportedException
	 
	 Console.WriteLine("---")
	 Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile is 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) 

      End Try
      
      Dim sr As New StreamReader(myResourceFS, System.Text.Encoding.ASCII)
      Console.WriteLine(sr.ReadToEnd())
      sr.Close()

   End Sub 'Main 

End Class 'AsmBuilderGetFileDemo

Comentarios

fileName no se debe ser el mismo que el de cualquier otro módulo con persistencia, recurso administrado independiente o el archivo de manifiesto independiente.fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.

Se supone que los recursos administrados en el archivo sea público.The managed resources in the file are assumed to be public.

El archivo de recursos especificado debe estar en el directorio donde se guardará el ensamblado.The specified resource file must be in the directory where the assembly will be saved.

Nota

A partir de la .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, ya no requiere este miembro ReflectionPermission con el ReflectionPermissionFlag.ReflectionEmit marca.Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (Consulte problemas de seguridad en Reflection emiten.) Para utilizar esta funcionalidad, la aplicación debe utilizar .NET Framework 3,5.NET Framework 3.5 o posterior como destino.(See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3,5.NET Framework 3.5 or later.

Seguridad

ReflectionPermission
Cuando se invoca en tiempo de ejecución a través de mecanismos como InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). Enumeración asociada: MemberAccess.Associated enumeration: MemberAccess.

FileIOPermission
El permiso FileIOPermissionAccess.Read es necesario tener acceso al archivo de recursos resourceFileName.The FileIOPermissionAccess.Read permission is needed to access the resource file resourceFileName.

AddResourceFile(String, String, ResourceAttributes) AddResourceFile(String, String, ResourceAttributes) AddResourceFile(String, String, ResourceAttributes) AddResourceFile(String, String, ResourceAttributes)

Agrega un archivo de recursos existente a este ensamblado.Adds an existing resource file to this assembly.

public:
 void AddResourceFile(System::String ^ name, System::String ^ fileName, System::Reflection::ResourceAttributes attribute);
public void AddResourceFile (string name, string fileName, System.Reflection.ResourceAttributes attribute);
member this.AddResourceFile : string * string * System.Reflection.ResourceAttributes -> unit
Public Sub AddResourceFile (name As String, fileName As String, attribute As ResourceAttributes)

Parámetros

name
String String String String

Nombre lógico del recurso.The logical name of the resource.

fileName
String String String String

Nombre de archivo físico (archivo .resources) al que se asigna el nombre lógico.The physical file name (.resources file) to which the logical name is mapped. Este no debe incluir una ruta de acceso; el archivo debe estar en el mismo directorio que el ensamblado al que se agrega.This should not include a path; the file must be in the same directory as the assembly to which it is added.

attribute
ResourceAttributes ResourceAttributes ResourceAttributes ResourceAttributes

Atributos de recursos.The resource attributes.

Excepciones

Se ha definido previamente name.name has been previously defined.

O bien-or- Hay otro archivo en el ensamblado denominado fileName.There is another file in the assembly named fileName.

O bien-or- La longitud de name es cero o si la longitud de fileName es cero.The length of name is zero or if the length of fileName is zero.

O bien-or- fileName incluye una ruta de acceso.fileName includes a path.

Si no se encuentra el archivo fileName.If the file fileName is not found.

El llamador no dispone del permiso requerido.The caller does not have the required permission.

Ejemplos

Ejemplo de código siguiente muestra cómo asociar un archivo de recursos a un ensamblado creado dinámicamente, utiliza AddResourceFile.The following code sample demonstrates how to attach a resource file to a dynamically created assembly, using AddResourceFile.

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class AsmBuilderGetFileDemo
{
public:
   static String^ myResourceFileName = "MyResource.txt";
   static FileInfo^ CreateResourceFile()
   {
      FileInfo^ f = gcnew FileInfo( myResourceFileName );
      StreamWriter^ sw = f->CreateText();
      sw->WriteLine( "Hello, world!" );
      sw->Close();
      return f;
   }

   static AssemblyBuilder^ BuildDynAssembly()
   {
      String^ myAsmFileName = "MyAsm.dll";
      AppDomain^ myDomain = Thread::GetDomain();
      AssemblyName^ myAsmName = gcnew 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;
   }

};

int main()
{
   FileStream^ myResourceFS = nullptr;
   AsmBuilderGetFileDemo::CreateResourceFile();
   Console::WriteLine( "The contents of MyResource.txt, via GetFile:" );
   AssemblyBuilder^ myAsm = AsmBuilderGetFileDemo::BuildDynAssembly();
   try
   {
      myResourceFS = myAsm->GetFile( AsmBuilderGetFileDemo::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 = gcnew FileStream( AsmBuilderGetFileDemo::myResourceFileName,FileMode::Open );
   }

   StreamReader^ sr = gcnew StreamReader( myResourceFS,System::Text::Encoding::ASCII );
   Console::WriteLine( sr->ReadToEnd() );
   sr->Close();
}


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();

   }

}


Imports System
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit

 _

Class AsmBuilderGetFileDemo
   
   Private Shared myResourceFileName As String = "MyResource.txt"
   
   
   Private Shared Function CreateResourceFile() As FileInfo
      
      Dim f As New FileInfo(myResourceFileName)
      Dim sw As StreamWriter = f.CreateText()
      
      sw.WriteLine("Hello, world!")
      
      sw.Close()
      
      Return f

   End Function 'CreateResourceFile
    
   
   Private Shared Function BuildDynAssembly() As AssemblyBuilder
      
      Dim myAsmFileName As String = "MyAsm.dll"
      
      Dim myDomain As AppDomain = Thread.GetDomain()
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = "MyDynamicAssembly"
      
      Dim myAsmBuilder As AssemblyBuilder = 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

   End Function 'BuildDynAssembly
    
   
   Public Shared Sub Main()
      
      Dim myResourceFS As FileStream = Nothing
      
      CreateResourceFile()
      
      Console.WriteLine("The contents of MyResource.txt, via GetFile:")
      
      Dim myAsm As AssemblyBuilder = BuildDynAssembly()
      
      Try

         myResourceFS = myAsm.GetFile(myResourceFileName)

      Catch nsException As NotSupportedException
	 
	 Console.WriteLine("---")
	 Console.WriteLine("System.Reflection.Emit.AssemblyBuilder.GetFile is 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) 

      End Try
      
      Dim sr As New StreamReader(myResourceFS, System.Text.Encoding.ASCII)
      Console.WriteLine(sr.ReadToEnd())
      sr.Close()

   End Sub 'Main 

End Class 'AsmBuilderGetFileDemo

Comentarios

fileName no se debe ser el mismo que el de cualquier otro módulo con persistencia, recurso administrado independiente o el archivo de manifiesto independiente.fileName should not be the same as that of any other persistable module, standalone managed resource, or the standalone manifest file.

Se pueden especificar atributos para el recurso administrado.Attributes can be specified for the managed resource.

El archivo de recursos especificado debe estar en el directorio donde se guardará el ensamblado.The specified resource file must be in the directory where the assembly will be saved.

Nota

A partir de la .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, ya no requiere este miembro ReflectionPermission con el ReflectionPermissionFlag.ReflectionEmit marca.Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (Consulte problemas de seguridad en Reflection emiten.) Para utilizar esta funcionalidad, la aplicación debe utilizar .NET Framework 3,5.NET Framework 3.5 o posterior como destino.(See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3,5.NET Framework 3.5 or later.

Seguridad

ReflectionPermission
Cuando se invoca en tiempo de ejecución a través de mecanismos como InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).when invoked late-bound through mechanisms such as InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]). Enumeración asociada: MemberAccess.Associated enumeration: MemberAccess.

FileIOPermission
El permiso FileIOPermissionAccess.Read es necesario tener acceso al archivo de recursos resourceFileName.The FileIOPermissionAccess.Read permission is needed to access the resource file resourceFileName.

Se aplica a