ResXResourceWriter ResXResourceWriter ResXResourceWriter ResXResourceWriter Class

定義

寫入資源至 XML 資源 (.resx) 檔或輸出資料流。Writes resources in an XML resource (.resx) file or an output stream.

public ref class ResXResourceWriter : System::Resources::IResourceWriter
public class ResXResourceWriter : System.Resources.IResourceWriter
type ResXResourceWriter = class
    interface IResourceWriter
    interface IDisposable
Public Class ResXResourceWriter
Implements IResourceWriter
繼承
ResXResourceWriterResXResourceWriterResXResourceWriterResXResourceWriter
實作

範例

下列範例會建立名為的.resx 檔案CarResources.resx可儲存六個字串、 圖示和兩個應用程式定義的物件 (兩個Automobile物件)。The following example creates a .resx file named CarResources.resx that stores six strings, an icon, and two application-defined objects (two Automobile objects). 請注意,範例中所定義和具現化的 Automobile 類別,會標記為 SerializableAttribute 屬性。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

備註

ResXResourceWriter以 XML 格式寫入資源。The ResXResourceWriter writes resources in XML format. 寫入二進位資源檔,請使用ResourceWriterTo write a binary resource file, use ResourceWriter.

資源會指定為名稱/值組,使用AddResource方法。Resources are specified as name/value pairs using the AddResource method. 資源名稱是用於查閱; 時,區分大小寫。但若要更輕鬆地支援撰寫工具,並協助消除 bugResXResourceWriter不允許 a.resx 檔案只有大小寫不同的名稱。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.

若要建立 a.resx 檔案,建立ResXResourceWriter唯一的檔案名稱,然後呼叫AddResource至少一次,呼叫Generate寫入磁碟中的資源檔,然後呼叫Close關閉檔案。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. 呼叫Close會隱含地Generate如有必要的檔案。Calling Close will implicitly Generate the file if required.

資源不一定會寫入其已加入的順序相同。The resources are not necessarily written in the same order they were added.

多個格式 a.resx 檔案的詳細資訊,請參閱使用.resx 檔案以程式設計方式For more information on the format of a.resx file, see Working with .resx Files Programmatically.

注意

這個類別包含的連結要求和繼承要求套用到所有成員的類別層級。This class contains a link demand and an inheritance demand at the class level that applies to all members. ASecurityException立即呼叫端或衍生的類別不具有完全信任權限時,會擲回。A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission.

建構函式

ResXResourceWriter(Stream) ResXResourceWriter(Stream) ResXResourceWriter(Stream) ResXResourceWriter(Stream)

初始化 ResXResourceWriter 類別的新執行個體,這個執行個體可將資源寫入指定的資料流物件。Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified stream object.

ResXResourceWriter(Stream, Func<Type,String>) ResXResourceWriter(Stream, Func<Type,String>) ResXResourceWriter(Stream, Func<Type,String>) ResXResourceWriter(Stream, Func<Type,String>)

初始化 ResXResourceWriter 類別的新執行個體,這個執行個體可將資源寫入指定的資料流物件以及設定轉換子委派。Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified stream object and sets a converter delegate. 此委派可讓資源組件使用完整組件名稱,以 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本為目標來撰寫。This delegate enables resource assemblies to be written that target versions of the .NET Framework before the .NET Framework 4.NET Framework 4 by using qualified assembly names.

ResXResourceWriter(String) ResXResourceWriter(String) ResXResourceWriter(String) ResXResourceWriter(String)

初始化 ResXResourceWriter 類別的新執行個體,這個執行個體會將資源寫入指定的檔案。Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified file.

ResXResourceWriter(String, Func<Type,String>) ResXResourceWriter(String, Func<Type,String>) ResXResourceWriter(String, Func<Type,String>) ResXResourceWriter(String, Func<Type,String>)

初始化 ResXResourceWriter 類別的新執行個體,這個執行個體可將資源寫入指定的檔案以及設定委派,以 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本為目標,讓資源組件使用完整組件名稱撰寫。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 the .NET Framework 4.NET Framework 4 by using qualified assembly names.

ResXResourceWriter(TextWriter) ResXResourceWriter(TextWriter) ResXResourceWriter(TextWriter) ResXResourceWriter(TextWriter)

初始化 ResXResourceWriter 類別的新執行個體,這個執行個體可將資源寫入指定的 TextWriter 物件。Initializes a new instance of the ResXResourceWriter class that writes to the specified TextWriter object.

ResXResourceWriter(TextWriter, Func<Type,String>) ResXResourceWriter(TextWriter, Func<Type,String>) ResXResourceWriter(TextWriter, Func<Type,String>) ResXResourceWriter(TextWriter, Func<Type,String>)

初始化 ResXResourceWriter 類別的新執行個體,這個執行個體可將資源寫入指定的 TextWriter 物件以及設定委派,以 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本為目標,讓資源組件使用完整組件名稱撰寫。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 the .NET Framework 4.NET Framework 4 by using qualified assembly names.

欄位

BinSerializedObjectMimeType BinSerializedObjectMimeType BinSerializedObjectMimeType BinSerializedObjectMimeType

指定二進位物件的預設內容類型。Specifies the default content type for a binary object. 此欄位為唯讀。This field is read-only.

ByteArraySerializedObjectMimeType ByteArraySerializedObjectMimeType ByteArraySerializedObjectMimeType ByteArraySerializedObjectMimeType

指定位元組陣列物件的預設內容類型。Specifies the default content type for a byte array object. 此欄位為唯讀。This field is read-only.

DefaultSerializedObjectMimeType DefaultSerializedObjectMimeType DefaultSerializedObjectMimeType DefaultSerializedObjectMimeType

指定物件的預設內容類型。Specifies the default content type for an object. 此欄位為唯讀。This field is read-only.

ResMimeType ResMimeType ResMimeType ResMimeType

指定 XML 資源的內容類型。Specifies the content type of an XML resource. 此欄位為唯讀。This field is read-only.

ResourceSchema ResourceSchema ResourceSchema ResourceSchema

指定寫入 XML 檔案時使用的結構描述。Specifies the schema to use in writing the XML file. 此欄位為唯讀。This field is read-only.

SoapSerializedObjectMimeType SoapSerializedObjectMimeType SoapSerializedObjectMimeType SoapSerializedObjectMimeType

指定 SOAP 物件的內容類型。Specifies the content type for a SOAP object. 此欄位為唯讀。This field is read-only.

Version Version Version Version

指定 XML 輸出確認的結構描述 (Schema) 版本。Specifies the version of the schema that the XML output conforms to. 此欄位為唯讀。This field is read-only.

屬性

BasePath BasePath BasePath BasePath

取得或設定 ResXFileRef 物件中指定之相對檔案路徑的基底路徑。Gets or sets the base path for the relative file path specified in a ResXFileRef object.

方法

AddAlias(String, AssemblyName) AddAlias(String, AssemblyName) AddAlias(String, AssemblyName) AddAlias(String, AssemblyName)

將指定的別名 (Alias) 加入別名清單。Adds the specified alias to a list of aliases.

AddMetadata(String, Byte[]) AddMetadata(String, Byte[]) AddMetadata(String, Byte[]) 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) AddMetadata(String, Object) AddMetadata(String, Object) 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) AddMetadata(String, String) AddMetadata(String, String) AddMetadata(String, String)

將其值指定為字串的設計階段屬性,加入要寫入的資源清單。Adds a design-time property whose value is specified as a string to the list of resources to write.

AddResource(ResXDataNode) AddResource(ResXDataNode) AddResource(ResXDataNode) AddResource(ResXDataNode)

ResXDataNode 物件中指定的具名資源,加入要寫入的資源清單。Adds a named resource specified in a ResXDataNode object to the list of resources to write.

AddResource(String, Byte[]) AddResource(String, Byte[]) AddResource(String, Byte[]) AddResource(String, Byte[])

將指定為位元組陣列的具名資源加入要寫入的資源清單。Adds a named resource specified as a byte array to the list of resources to write.

AddResource(String, Object) AddResource(String, Object) AddResource(String, Object) AddResource(String, Object)

將指定為物件的具名資源加入要寫入的資源清單。Adds a named resource specified as an object to the list of resources to write.

AddResource(String, String) AddResource(String, String) AddResource(String, String) AddResource(String, String)

加入字串資源到資源中。Adds a string resource to the resources.

Close() Close() Close() Close()

釋放 ResXResourceWriter 所使用的所有資源。Releases all resources used by the ResXResourceWriter.

Dispose() Dispose() Dispose() Dispose()

釋放 ResXResourceWriter 所使用的所有資源。Releases all resources used by the ResXResourceWriter.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 ResXResourceWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the ResXResourceWriter and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

這個成員會覆寫 Finalize() 方法。This member overrides the Finalize() method.

Generate() Generate() Generate() Generate()

寫入所有由 AddResource(String, Byte[]) 方法加入的資源至輸出檔或資料流。Writes all resources added by the AddResource(String, Byte[]) method to the output file or stream.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

安全性

LinkDemand
針對完全信任立即呼叫端。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

InheritanceDemand
針對繼承者完全信任。for full trust for inheritors. 這個成員無法被繼承,部分信任程式碼。This member cannot be inherited by partially trusted code.

適用於

另請參閱