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 文件。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. 资源名称在用于查找时区分大小写,但为了更轻松地支持创作工具并帮助消除 bug, ResourceWriter将不允许 .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)

适用于

另请参阅