ResourceWriter.AddResource Method

Definition

Adds a resource to the list of resources to be written.

Overloads

AddResource(String, Byte[])

Adds a named resource specified as a byte array 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, Object)

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

AddResource(String, String)

Adds a string resource 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, Byte[])

Source:
ResourceWriter.cs
Source:
ResourceWriter.cs
Source:
ResourceWriter.cs

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

C#
public void AddResource(string name, byte[]? value);
C#
public void AddResource(string name, byte[] value);

Parameters

name
String

The name of the resource.

value
Byte[]

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

Implements

Exceptions

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

The name parameter is null.

This ResourceWriter has been closed and its hash table is unavailable.

Examples

The following example uses the AddResource(String, Byte[]) method to add a graphics image that has been read as an array of bytes to a ResourceWriter object.

C#
using System;
using System.IO;
using System.Resources;

public class Example
{
   public static void Main()
   {
      // Get the image as an array of bytes.
      FileStream byteStream = new FileStream("AppIcon.jpg", FileMode.Open);
      Byte[] bytes = new Byte[(int) byteStream.Length];
      byteStream.Read(bytes, 0, (int) byteStream.Length);
      
      // Create the resource file.
      using (ResourceWriter rw = new ResourceWriter(@".\UIImages.resources")) {
         rw.AddResource("AppIcon", byteStream);
         // Add any other resources.
      }
   }
}

Remarks

The resource is not written until Generate is called.

You can retrieve the resources written by the AddResource(String, Byte[]) method by calling the ResourceManager.GetStream method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

AddResource(String, Stream)

Source:
ResourceWriter.core.cs
Source:
ResourceWriter.core.cs
Source:
ResourceWriter.core.cs

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

C#
public void AddResource(string name, System.IO.Stream? value);
C#
public void AddResource(string name, System.IO.Stream value);

Parameters

name
String

The name of the resource to add.

value
Stream

The value of the resource to add. The resource must support the Length property.

Exceptions

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

-or-

The stream does not support the Length property.

name or value is null.

Examples

The following example uses the AddResource(String, Stream) method to add a graphics image that has been saved to a MemoryStream object to a ResourceWriter object.

C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Resources;

public class Example
{
   public static void Main()
   {
      // Bitmap as stream
      MemoryStream bitmapStream = new MemoryStream();
      Bitmap bmp = new Bitmap(@".\\AppImage.jpg");
      bmp.Save(bitmapStream, ImageFormat.Jpeg);
          
      using (ResourceWriter rw = new ResourceWriter(@".\UIImages.resources"))
      {
         rw.AddResource("Bitmap", bitmapStream);
         // Add other resources.
      }
   }
}

Remarks

You can specify any stream that supports the Stream.Length property for value.

You can retrieve the resources written by the AddResource(String, Stream) method by calling the ResourceManager.GetStream method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

AddResource(String, Object)

Source:
ResourceWriter.cs
Source:
ResourceWriter.cs
Source:
ResourceWriter.cs

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

C#
public void AddResource(string name, object? value);
C#
public void AddResource(string name, object value);

Parameters

name
String

The name of the resource.

value
Object

The value of the resource.

Implements

Exceptions

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

The name parameter is null.

This ResourceWriter has been closed and its hash table is unavailable.

Examples

The following example uses the AddResource(String, Object) method to add object data to a binary resources file.

C#
using System;
using System.Resources;

public class Example
{
   public static void Main()
   {
      DonorColumns columns = new DonorColumns("Emplyee #", "Name", 
                                              "Total Amount", "Last Donation Date",
                                              "Last Donation Amount");
      ResourceWriter resFile = new ResourceWriter(@".\UIResources.resources");
      resFile.AddResource("Title", "Corporate Gold Star Donors");
      resFile.AddResource("NColumns", 5);
      resFile.AddResource("AppDate", new DateTime(2011, 5, 28));
      resFile.AddResource("AppVersion", new Version(1, 0, 217));
      resFile.AddResource("HRVersion", true);
      resFile.Generate();
      resFile.Close();               
   }
}

// Class to hold potentially localized column names.
[Serializable] public class DonorColumns
{
   readonly string ID;
   readonly string Name;
   readonly string Total;
   readonly string Last;
   readonly string Amt;

   public DonorColumns(string id, string name, string total, 
                  string last, string amt)
   {                  
      this.ID = id;
      this.Name = name;
      this.Total = total;
      this.Last = last;
      this.Amt = amt;                        
   }   
}

DonorColumns is a custom class whose fields contain the names of columns to be displayed in the user interface. Note that the class is marked with the SerializableAttribute attribute. Ordinarily, the class would be defined in a separate assembly, and a reference to it would be provided to the compiler at compile time.

Remarks

value must be serializable.

The resource is not written until the Generate method is called.

You can retrieve the resources written by the AddResource(String, Object) method by calling the ResourceManager.GetObject method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

AddResource(String, String)

Source:
ResourceWriter.cs
Source:
ResourceWriter.cs
Source:
ResourceWriter.cs

Adds a string resource to the list of resources to be written.

C#
public void AddResource(string name, string? value);
C#
public void AddResource(string name, string value);

Parameters

name
String

The name of the resource.

value
String

The value of the resource.

Implements

Exceptions

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

The name parameter is null.

This ResourceWriter has been closed and its hash table is unavailable.

Examples

The following example uses the AddResource method to add string resources to a ResourceWriter object.

C#
using System;
using System.Resources;
using System.IO;

public class WriteResources 
{
    public static void Main(string[] args) 
    {  
        // Create a file stream to encapsulate items.resources.
        FileStream fs = new FileStream("items.resources", 
        FileMode.OpenOrCreate,FileAccess.Write);

        // Open a resource writer to write from the stream.
        IResourceWriter writer = new ResourceWriter(fs);
    
        // Add resources to the resource writer.
        writer.AddResource("String 1", "First String");
        writer.AddResource("String 2", "Second String");
        writer.AddResource("String 3", "Third String");

        // Write the resources to the stream, and close it.
        writer.Close();
    }
}

Remarks

The resource is not written until Generate is called.

You can retrieve the resources written by the AddResource(String, String) method by calling the ResourceManager.GetString method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

AddResource(String, Stream, Boolean)

Source:
ResourceWriter.cs
Source:
ResourceWriter.cs
Source:
ResourceWriter.cs

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.

C#
public void AddResource(string name, System.IO.Stream? value, bool closeAfterWrite = false);
C#
public void AddResource(string name, System.IO.Stream value, bool closeAfterWrite);

Parameters

name
String

The name of the resource to add.

value
Stream

The value of the resource to add. The resource must support the Length property.

closeAfterWrite
Boolean

true to close the stream after the Generate() method is called; otherwise, false.

Exceptions

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

-or-

The stream does not support the Length property.

name or value is null.

Examples

The following example uses the AddResource(String, Stream, Boolean) method to add a graphics image that has been saved to a MemoryStream object to a ResourceWriter object.

C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Resources;

public class Example
{
   public static void Main()
   {
      // Bitmap as stream
      MemoryStream bitmapStream = new MemoryStream();
      Bitmap bmp = new Bitmap(@".\\AppImage.jpg");
      bmp.Save(bitmapStream, ImageFormat.Jpeg);
          
      ResourceWriter rw = new ResourceWriter(@".\UIImages.resources");
      rw.AddResource("Bitmap", bitmapStream, true);
      // Add other resources.
      rw.Generate();
   }
}

Remarks

You can specify any stream that supports the Stream.Length property for value.

You can retrieve the resources written by the AddResource(String, Stream, Boolean) method by calling the ResourceManager.GetStream method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1