Bewerken

Share via


ResXResourceWriter Class

Definition

Writes resources in an XML resource (.resx) file or an output stream.

public ref class ResXResourceWriter : System::Resources::IResourceWriter
public ref class ResXResourceWriter : IDisposable, System::Resources::IResourceWriter
public class ResXResourceWriter : System.Resources.IResourceWriter
public class ResXResourceWriter : IDisposable, System.Resources.IResourceWriter
type ResXResourceWriter = class
    interface IResourceWriter
    interface IDisposable
Public Class ResXResourceWriter
Implements IResourceWriter
Public Class ResXResourceWriter
Implements IDisposable, IResourceWriter
Inheritance
ResXResourceWriter
Implements

Examples

The following example creates a .resx file named CarResources.resx that stores six strings, an icon, and two application-defined objects (two Automobile objects). Note that the Automobile class, which is defined and instantiated in the example, is tagged with the SerializableAttribute attribute.

using System;
using System.Drawing;
using System.Resources;

[Serializable()] public class Automobile
{
   private string carMake;
   private string carModel;
   private int carYear;
   private int carDoors;
   private int carCylinders;

   public Automobile(string make, string model, int year) :
                     this(make, model, year, 0, 0)
   { }

   public Automobile(string make, string model, int year,
                     int doors, int cylinders)
   {
      this.carMake = make;
      this.carModel = model;
      this.carYear = year;
      this.carDoors = doors;
      this.carCylinders = cylinders;
   }

   public string Make {
      get { return this.carMake; }
   }

   public string Model {
      get {return this.carModel; }
   }

   public int Year {
      get { return this.carYear; }
   }

   public int Doors {
      get { return this.carDoors; }
   }

   public int Cylinders {
      get { return this.carCylinders; }
   }
}

public class Example
{
   public static void Main()
   {
      // Instantiate an Automobile object.
      Automobile car1 = new Automobile("Ford", "Model N", 1906, 0, 4);
      Automobile car2 = new Automobile("Ford", "Model T", 1909, 2, 4);
      // Define a resource file named CarResources.resx.
      using (ResXResourceWriter resx = new ResXResourceWriter(@".\CarResources.resx"))
      {
         resx.AddResource("Title", "Classic American Cars");
         resx.AddResource("HeaderString1", "Make");
         resx.AddResource("HeaderString2", "Model");
         resx.AddResource("HeaderString3", "Year");
         resx.AddResource("HeaderString4", "Doors");
         resx.AddResource("HeaderString5", "Cylinders");
         resx.AddResource("Information", SystemIcons.Information);
         resx.AddResource("EarlyAuto1", car1);
         resx.AddResource("EarlyAuto2", car2);
      }
   }
}
Imports System.Drawing
Imports System.Resources

<Serializable()> Public Class Automobile
   Private carMake As String
   Private carModel As String
   Private carYear As Integer
   Private carDoors AS Integer
   Private carCylinders As Integer
   
   Public Sub New(make As String, model As String, year As Integer) 
      Me.New(make, model, year, 0, 0)   
   End Sub
   
   Public Sub New(make As String, model As String, year As Integer, 
                  doors As Integer, cylinders As Integer)
      Me.carMake = make
      Me.carModel = model
      Me.carYear = year
      Me.carDoors = doors
      Me.carCylinders = cylinders
   End Sub

   Public ReadOnly Property Make As String
      Get
         Return Me.carMake
      End Get   
   End Property       
   
   Public ReadOnly Property Model As String
      Get
         Return Me.carModel
      End Get   
   End Property       
   
   Public ReadOnly Property Year As Integer
      Get
         Return Me.carYear
      End Get   
   End Property       
   
   Public ReadOnly Property Doors As Integer
      Get
         Return Me.carDoors
      End Get   
   End Property       
   
   Public ReadOnly Property Cylinders As Integer
      Get
         Return Me.carCylinders
      End Get   
   End Property       
End Class

Module Example
   Public Sub Main()
      ' Instantiate an Automobile object.
      Dim car1 As New Automobile("Ford", "Model N", 1906, 0, 4)
      Dim car2 As New Automobile("Ford", "Model T", 1909, 2, 4)
      ' Define a resource file named CarResources.resx.
      Using resx As New ResXResourceWriter(".\CarResources.resx")
         resx.AddResource("Title", "Classic American Cars")
         resx.AddResource("HeaderString1", "Make")
         resx.AddResource("HeaderString2", "Model")
         resx.AddResource("HeaderString3", "Year")
         resx.AddResource("HeaderString4", "Doors")
         resx.AddResource("HeaderString5", "Cylinders")
         resx.AddResource("Information", SystemIcons.Information) 
         resx.AddResource("EarlyAuto1", car1)
         resx.AddResource("EarlyAuto2", car2)  
      End Using
   End Sub
End Module

Remarks

The ResXResourceWriter writes resources in XML format. To write a binary resource file, use ResourceWriter.

Resources are specified as name/value pairs using the AddResource method. Resource names are case-sensitive when used for lookups; but to more easily support authoring tools and help eliminate bugs, ResXResourceWriter does not allow a.resx file to have names that vary only by case.

To create a.resx file, create a ResXResourceWriter with a unique file name, call AddResource at least once, call Generate to write the resources file to disk, and then call Close to close the file. Calling Close will implicitly Generate the file if required.

The resources are not necessarily written in the same order they were added.

For more information on the format of a.resx file, see Working with .resx Files Programmatically.

Note

This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission.

Constructors

ResXResourceWriter(Stream, Func<Type,String>)

Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified stream object and sets a converter delegate. This delegate enables resource assemblies to be written that target versions of the .NET Framework before .NET Framework 4 by using qualified assembly names.

ResXResourceWriter(Stream)

Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified stream object.

ResXResourceWriter(String, Func<Type,String>)

Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified file and sets a delegate that enables resource assemblies to be written that target versions of the .NET Framework before .NET Framework 4 by using qualified assembly names.

ResXResourceWriter(String)

Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified file.

ResXResourceWriter(TextWriter, Func<Type,String>)

Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified TextWriter object and sets a delegate that enables resource assemblies to be written that target versions of the .NET Framework before .NET Framework 4 by using qualified assembly names.

ResXResourceWriter(TextWriter)

Initializes a new instance of the ResXResourceWriter class that writes to the specified TextWriter object.

Fields

BinSerializedObjectMimeType

Specifies the default content type for a binary object. This field is read-only.

ByteArraySerializedObjectMimeType

Specifies the default content type for a byte array object. This field is read-only.

DefaultSerializedObjectMimeType

Specifies the default content type for an object. This field is read-only.

ResMimeType

Specifies the content type of an XML resource. This field is read-only.

ResourceSchema

Specifies the schema to use in writing the XML file. This field is read-only.

SoapSerializedObjectMimeType

Specifies the content type for a SOAP object. This field is read-only.

Version

Specifies the version of the schema that the XML output conforms to. This field is read-only.

Properties

BasePath

Gets or sets the base path for the relative file path specified in a ResXFileRef object.

Methods

AddAlias(String, AssemblyName)

Adds the specified alias to a list of aliases.

AddMetadata(String, Byte[])

Adds a design-time property whose value is specified as a byte array to the list of resources to write.

AddMetadata(String, Object)

Adds a design-time property whose value is specified as an object to the list of resources to write.

AddMetadata(String, String)

Adds a design-time property whose value is specified as a string to the list of resources to write.

AddResource(ResXDataNode)

Adds a named resource specified in a ResXDataNode object to the list of resources to write.

AddResource(String, Byte[])

Adds a named resource specified as a byte array to the list of resources to write.

AddResource(String, Object)

Adds a named resource specified as an object to the list of resources to write.

AddResource(String, String)

Adds a string resource to the resources.

Close()

Releases all resources used by the ResXResourceWriter.

Dispose()

Releases all resources used by the ResXResourceWriter.

Dispose(Boolean)

Releases the unmanaged resources used by the ResXResourceWriter and optionally releases the managed resources.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize()

This member overrides the Finalize() method.

Generate()

Writes all resources added by the AddResource(String, Byte[]) method to the output file or stream.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also