System.Resources.ResourceWriter Class

Assembly: Mscorlib.dll
Namespace: System.Resources
Summary
Writes resources in the system-default format to an output file or an output stream.
C# Syntax:
public sealed class ResourceWriter : IResourceWriter, IDisposable
Remarks
ResourceWriter provides a default implementation of the IResourceWriter interface.

Resources are specified as name and value pairs using the ResourceWriter.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.

To create a resources file, create a ResourceWriter with a unique file name, call ResourceWriter.AddResource at least once, call ResourceWriter.Generate to write the resources file to disk, and then call ResourceWriter.Close to close the file. Calling ResourceWriter.Close will implicitly ResourceWriter.Generate the file if required.

The resources will not necessarily be written in the same order they were added.

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

    
See also:
System.Resources Namespace See also:
MSDN: designingglobalapplications | IResourceWriter | IDisposable

System.Resources.ResourceWriter Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(Stream stream)

Initializes a new instance of the ResourceWriter class that writes the resources to the provided stream.
ctor #2 Overloaded:
.ctor(string fileName)

Initializes a new instance of the ResourceWriter class that writes the resources to the specified file.
Public Methods
AddResource Overloaded:
AddResource(string name, byte[] value)

Adds a named resource specified as a byte array to the list of resources to be written.
AddResource Overloaded:
AddResource(string name, object value)

Adds a named resource specified as an object to the list of resources to be written.
AddResource Overloaded:
AddResource(string name, string value)

Adds a String 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
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
Generate Saves all resources to the output stream in the system default format.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Resources.ResourceWriter Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the ResourceWriter class that writes the resources to the provided stream.
C# Syntax:
public ResourceWriter(
   Stream stream
);
Parameters:

stream

The output stream.

Exceptions
Exception Type Condition
ArgumentException The stream parameter is not writable.
ArgumentNullException The stream parameter is null.
See also:
MSDN: designingglobalapplications

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the ResourceWriter class that writes the resources to the specified file.
C# Syntax:
public ResourceWriter(
   string fileName
);
Parameters:

fileName

The output file name.

Exceptions
Exception Type Condition
ArgumentNullException The fileName parameter is null.
See also:
MSDN: designingglobalapplications

Return to top


Overloaded Method: AddResource(
   string name,
   byte[] value
)
Summary
Adds a named resource specified as a byte array to the list of resources to be written.
C# Syntax:
public void AddResource(
   string name,
   byte[] value
);
Parameters:

name

The name of the resource.

value

Value of the resource as an 8-bit unsigned integer array.

Exceptions
Exception Type Condition
ArgumentException name (or a name that varies only by capitalization) has already been added to this ResourceWriter.
ArgumentNullException The name or value parameter is null.
InvalidOperationException This ResourceWriter has been closed and its Hashtable is unavailable.
Implements:
IResourceWriter.AddResource
Remarks
The resource is not written until ResourceWriter.Generate is called.

Return to top


Overloaded Method: AddResource(
   string name,
   object value
)
Summary
Adds a named resource specified as an object to the list of resources to be written.
C# Syntax:
public void AddResource(
   string name,
   object value
);
Parameters:

name

The name of the resource.

value

The value of the resource.

Exceptions
Exception Type Condition
ArgumentException name (or a name that varies only by capitalization) has already been added to this ResourceWriter.
ArgumentNullException The name or value parameter is null.
InvalidOperationException This ResourceWriter has been closed and its Hashtable is unavailable.
Implements:
IResourceWriter.AddResource
Remarks
The resource is not written until ResourceWriter.Generate is called. The resource that was added must be serializable.

Return to top


Overloaded Method: AddResource(
   string name,
   string value
)
Summary
Adds a String resource to the list of resources to be written.
C# Syntax:
public void AddResource(
   string name,
   string value
);
Parameters:

name

The name of the resource.

value

The value of the resource.

Exceptions
Exception Type Condition
ArgumentException name (or a name that varies only by capitalization) has already been added to this ResourceWriter.
ArgumentNullException The name or value parameter is null.
InvalidOperationException This ResourceWriter has been closed and its Hashtable is unavailable.
Implements:
IResourceWriter.AddResource
Remarks
The resource is not written until ResourceWriter.Generate is called.

Return to top


Method: Close()
Summary
Saves the resources to the output stream and then closes it.
C# Syntax:
public void Close();
Exceptions
Exception Type Condition
IOException An I/O error has occurred.
SerializationException An error has occurred during serialization of the object.
Implements:
IResourceWriter.Close
Remarks
ResourceWriter.Generate is called implicitly by ResourceWriter.Close, if required.

Return to top


Method: Dispose()
Summary
Allows users to close the resource file or stream, explicitly releasing resources.
C# Syntax:
public void Dispose();
Exceptions
Exception Type Condition
IOException An I/O error has occurred.
SerializationException An error has occurred during serialization of the object.
Implements:
IDisposable.Dispose
Remarks
The implementation of this method is the same as ResourceWriter.Close.
See also:
IDisposable

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~ResourceWriter();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Generate()
Summary
Saves all resources to the output stream in the system default format.
C# Syntax:
public void Generate();
Exceptions
Exception Type Condition
IOException An I/O error occurred.
SerializationException An error occurred during serialization of the object.
InvalidOperationException This ResourceWriter has been closed and its Hashtable is unavailable.
Implements:
IResourceWriter.Generate
Remarks
ResourceWriter.Generate can only be called once, after all calls to ResourceWriter.AddResource have been made. If an exception occurs while writing the resources, the output stream is closed since the information written is most likely invalid.

ResourceWriter.Generate does not close the output stream in normal cases. Unless you are combining extra data with your .resources file or need access to the stream afterwards, you should call ResourceWriter.Close after calling ResourceWriter.Generate, or simply call ResourceWriter.Close.

See also:
MSDN: serialization

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.