ResourceWriter 類別

定義

將系統預設格式中的資源寫入輸出檔或輸出資料流。Writes resources in the system-default format to an output file or an output stream. 這個類別無法被繼承。This class cannot be inherited.

public ref class ResourceWriter sealed : System::Resources::IResourceWriter
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ResourceWriter : System.Resources.IResourceWriter
type ResourceWriter = class
    interface IResourceWriter
    interface IDisposable
Public NotInheritable Class ResourceWriter
Implements IResourceWriter
繼承
ResourceWriter
屬性
實作

範例

下列範例會將數個字串寫入 myResources .resources 檔案中。The following example writes several strings into the myResources.resources file.

using namespace System;
using namespace System::Resources;
int main()
{
   
   // Creates a resource writer.
   IResourceWriter^ writer = gcnew ResourceWriter( "myResources.resources" );
   
   // Adds resources to the resource writer.
   writer->AddResource( "String 1", "First String" );
   writer->AddResource( "String 2", "Second String" );
   writer->AddResource( "String 3", "Third String" );
   
   // Writes the resources to the file or stream, and closes it.
   writer->Close();
}

using System;
using System.Resources;


public class WriteResources {
   public static void Main(string[] args) {
      
      // Creates a resource writer.
      IResourceWriter writer = new ResourceWriter("myResources.resources");
    
      // Adds resources to the resource writer.
      writer.AddResource("String 1", "First String");

      writer.AddResource("String 2", "Second String");

      writer.AddResource("String 3", "Third String");

      // Writes the resources to the file or stream, and closes it.
      writer.Close();
   }
}
Imports System.Resources

Public Class WriteResources
    
    Public Shared Sub Main()
        
        ' Creates a resource writer.
        Dim writer As New ResourceWriter("myResources.resources")
        
        ' Adds resources to the resource writer.
        writer.AddResource("String 1", "First String")
        
        writer.AddResource("String 2", "Second String")
        
        writer.AddResource("String 3", "Third String")
        
        ' Writes the resources to the file or stream, and closes it.
        writer.Close()
    End Sub
End Class

備註

ResourceWriter提供IResourceWriter介面的預設執行。ResourceWriter provides a default implementation of the IResourceWriter interface. 它可讓您以程式設計方式建立二進位資源(.resources)檔案。It enables you to programmatically create a binary resource (.resources) file.

使用AddResource方法,將資源指定為名稱和值組。Resources are specified as name and value pairs using the AddResource method. 當資源名稱用於查閱時,會區分大小寫,但若要更輕鬆地支援撰寫工具並協助ResourceWriter消除 bug,將不允許 .resources 檔案擁有只有大小寫不同的名稱。Resource names are case-sensitive when used for lookups, but to more easily support authoring tools and help eliminate bugs, ResourceWriter will not allow a .resources file to have names that vary only by case. ResourceWriter類別可讓您建立字串、物件和二進位資源。The ResourceWriter class enables you to create string, object, and binary resources. 二進位資源可以寫入至資源檔做為位元組陣列或資料流程。Binary resources can be written to the resource file as a byte array or a stream.

重要

此型別代表 IDisposable 介面。This type implements the IDisposable interface. 當您完成使用型別時,您應該直接或間接處置它。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

若要建立資源ResourceWriter檔,請使用唯一的檔案名建立,再呼叫AddResource至少一次,呼叫Generate以將資源檔寫入磁片,然後呼叫Close以關閉檔案。To create a resources file, create a ResourceWriter 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呼叫Generate,則呼叫會隱含地呼叫。Calling Close will implicitly call Generate if you do not explicitly call Generate.

不一定會以新增的相同順序來寫入資源。The resources will not necessarily be written in the same order they were added.

若要從類別所ResourceWriter建立的二進位 .resources 檔案抓取資源,您可以ResourceManager使用類別,讓您抓取ResourceReader已命名的資源或類別,這可讓您列舉檔案中的所有資源。To retrieve resources from a binary .resources file created by the ResourceWriter class, you can use the ResourceManager class, which lets you retrieve named resources, or the ResourceReader class, which lets you enumerate all the resources in the file.

建構函式

ResourceWriter(Stream)

初始化 ResourceWriter 類別的新執行個體,這個執行個體可將資源寫入提供的資料流。Initializes a new instance of the ResourceWriter class that writes the resources to the provided stream.

ResourceWriter(String)

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

屬性

TypeNameConverter

取得或設定委派,這個委派可讓您使用完整組件名稱,撰寫以 .NET Framework 4.NET Framework 4 之前的 .NET Framework 版本為目標的資源組件。Gets or sets a delegate that enables resource assemblies to be written that target versions of the .NET Framework prior to the .NET Framework 4.NET Framework 4 by using qualified assembly names.

方法

AddResource(String, Byte[])

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

AddResource(String, Object)

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

AddResource(String, Stream)

加入具名資源,這個資源已指定為要寫入資源清單的資料流。Adds a named resource specified as a stream to the list of resources to be written.

AddResource(String, Stream, Boolean)

加入已指定為要寫入資源清單之資料流的具名資源,並指定該資料流是否應於 Generate() 方法呼叫之後關閉。Adds a named resource specified as a stream to the list of resources to be written, and specifies whether the stream should be closed after the Generate() method is called.

AddResource(String, String)

將字串資源加入至要寫入的資源清單。Adds a string resource to the list of resources to be written.

AddResourceData(String, String, Byte[])

以資源加入要寫入之資源清單的資料單位。Adds a unit of data as a resource to the list of resources to be written.

Close()

將資源儲存至輸出資料流,然後關閉它。Saves the resources to the output stream and then closes it.

Dispose()

允許使用者關閉資源檔或資料流,明確釋放資源。Allows users to close the resource file or stream, explicitly releasing resources.

Equals(Object)

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

(繼承來源 Object)
Generate()

以系統預設格式儲存所有資源至輸出資料流。Saves all resources to the output stream in the system default format.

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)

適用於

另請參閱