ResourceWriter Class

Writes resources in the system-default format to an output file or an output stream. This class cannot be inherited.

System.Object
  System.Resources.ResourceWriter

Namespace:  System.Resources
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)]
public sealed class ResourceWriter : IResourceWriter, 
	IDisposable

The ResourceWriter type exposes the following members.

  NameDescription
Public methodResourceWriter(Stream)Initializes a new instance of the ResourceWriter class that writes the resources to the provided stream.
Public methodResourceWriter(String)Initializes a new instance of the ResourceWriter class that writes the resources to the specified file.
Top

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

  NameDescription
Public methodAddResource(String, Byte[])Adds a named resource specified as a byte array to the list of resources to be written.
Public methodAddResource(String, Stream)Adds a named resource specified as a stream to the list of resources to be written.
Public methodAddResource(String, Object)Adds a named resource specified as an object to the list of resources to be written.
Public methodAddResource(String, String)Adds a string resource to the list of resources to be written.
Public methodAddResource(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.
Public methodAddResourceDataAdds a unit of data as a resource to the list of resources to be written.
Public methodCloseSaves the resources to the output stream and then closes it.
Public methodDisposeAllows users to close the resource file or stream, explicitly releasing resources.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGenerateSaves all resources to the output stream in the system default format.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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 noteImportant

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.

The following example writes several strings into the myResources.resources file.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft