ResXResourceWriter 類別

定義

寫入資源至 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
繼承
ResXResourceWriter
實作

範例

下列範例會建立名為CarResources.resx的 .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

備註

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

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

若要建立 .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.

如需 .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. 當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回。SecurityExceptionA SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission.

建構函式

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 類別的新執行個體,這個執行個體可將資源寫入指定的資料流物件以及設定轉換子委派。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 類別的新執行個體,這個執行個體會將資源寫入指定的檔案。Initializes a new instance of the ResXResourceWriter class that writes the resources to the specified file.

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 物件。Initializes a new instance of the ResXResourceWriter class that writes to the specified TextWriter object.

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

指定二進位物件的預設內容類型。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

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

ResourceSchema

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

SoapSerializedObjectMimeType

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

Version

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

屬性

BasePath

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

方法

AddAlias(String, AssemblyName)

將指定的別名 (Alias) 加入別名清單。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)

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

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

Dispose()

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

Dispose(Boolean)

釋放 ResXResourceWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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.

(繼承來源 Object)
Finalize()

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

Generate()

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

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

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

(繼承來源 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.

適用於

另請參閱