Export (0) Print
Expand All

ResourceWriter.AddResourceData Method

Adds a unit of data as a resource to the list of resources to be written.

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

public void AddResourceData(
	string name,
	string typeName,
	byte[] serializedData
)

Parameters

name
Type: System.String

A name that identifies the resource that contains the added data.

typeName
Type: System.String

The type name of the added data. For more information, see the Remarks section.

serializedData
Type: System.Byte[]

A byte array that contains the binary representation of the added data.

ExceptionCondition
ArgumentNullException

name, typeName, or serializedData is null.

ArgumentException

name (or a name that varies only by capitalization) has already been added to this ResourceWriter object.

InvalidOperationException

The current ResourceWriter object is not initialized. The probable cause is that the ResourceWriter object is closed.

Use the AddResourceData method to add a resource in binary form (that is, as an array of bytes) to the list of resources to be written. You must specify the name of the resource, the type name of the data contained in the resource, and the binary representation of the data itself. After you have added each resource you require, use the Generate method to write the list of resources to the resources file or stream that was specified in the ResourceWriter constructor.

typeName is a string that represents the data type of the resource. It can be any of the following values:

  • The string representation of a ResourceTypeCode enumeration member that indicates the data type of the resource. ResourceTypeCode is a private enumeration that is used by Resgen.exe to indicate that a special binary format is used to store one of 19 common data types. These include the .NET Framework primitive data types (Boolean, Byte, Char, Decimal, Double, Int16, Int32, Int64, Single, SByte, UInt16, UInt32, UInt64), as well as String, DateTime, and TimeSpan. In addition, the ResourceTypeCode enumeration includes the values shown in the following table.

    ResourceTypeCode value

    Description

    ResourceTypeCode.ByteArray

    The data is a byte array.

    ResourceTypeCode.Null

    The data is a null reference.

    ResourceTypeCode.Stream

    The data is stored in a stream.

  • A string that contains the fully qualified name of the type whose binary data is assigned to the serializedData argument (for example, System.String). In addition, for types that are not part of the .NET Framework class library, the string includes the name, version, culture, and public key of the assembly that contains the type. For example, the following string indicates that the serialized data represents an instance of the Person type in the Extensions namespace, which is found in version 1.0 of an assembly named Utility that has no public key and no designated culture.

    Extensions.Person, Utility, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

A parallel method for reading resource data written with the AddResourceData method is ResourceReader.GetResourceData.

The following example uses the AddResourceData method to write two integer values to a .resources file, and then uses a ResourceReader object to retrieve them.

using System;
using System.Collections;
using System.Resources;

public class Example
{
   public static void Main()
   {
      ResourceWriter rw = new ResourceWriter(@".\TypeResources.resources");
      int n1 = 1032;
      rw.AddResourceData("Integer1", "ResourceTypeCode.Int32", BitConverter.GetBytes(n1));
      int n2 = 2064;       
      rw.AddResourceData("Integer2", "ResourceTypeCode.Int32", BitConverter.GetBytes(n2));
      rw.Generate();
      rw.Close();

      ResourceReader rr = new ResourceReader(@".\TypeResources.resources");
      IDictionaryEnumerator e = rr.GetEnumerator();
      while (e.MoveNext())
         Console.WriteLine("{0}: {1}", e.Key, e.Value);
   }
}
// The example displays the following output: 
//       Integer2: 2064 
//       Integer1: 1032

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft