Bewerken

Share via


ResourceWriter Class

Definition

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 : IDisposable, System::Resources::IResourceWriter
public ref class ResourceWriter sealed : IDisposable
public ref class ResourceWriter sealed : System::Resources::IResourceWriter
public sealed class ResourceWriter : IDisposable, System.Resources.IResourceWriter
public sealed class ResourceWriter : IDisposable
public sealed class ResourceWriter : System.Resources.IResourceWriter
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ResourceWriter : System.Resources.IResourceWriter
type ResourceWriter = class
    interface IDisposable
    interface IResourceWriter
type ResourceWriter = class
    interface IDisposable
type ResourceWriter = class
    interface IResourceWriter
    interface IDisposable
[<System.Runtime.InteropServices.ComVisible(true)>]
type ResourceWriter = class
    interface IResourceWriter
    interface IDisposable
Public NotInheritable Class ResourceWriter
Implements IDisposable, IResourceWriter
Public NotInheritable Class ResourceWriter
Implements IDisposable
Public NotInheritable Class ResourceWriter
Implements IResourceWriter
Inheritance
ResourceWriter
Attributes
Implements

Examples

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

Remarks

ResourceWriter provides a default implementation of the IResourceWriter interface. It enables you to programmatically create a binary resource (.resources) file.

Resources are specified as name and 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, ResourceWriter will not allow a .resources file to have names that vary only by case. 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.

Important

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

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. 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.

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.

Constructors

ResourceWriter(Stream)

Initializes a new instance of the ResourceWriter class that writes the resources to the provided stream.

ResourceWriter(String)

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

Properties

TypeNameConverter

Gets or sets a delegate that enables resource assemblies to be written that target versions of .NET Framework prior to .NET Framework 4 by using qualified assembly names.

Methods

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, Boolean)

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, Stream)

Adds a named resource specified as a stream to the list of resources to be written.

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.

(Inherited from Object)
Generate()

Saves all resources to the output stream in the system default format.

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