AddResource Method (String, Stream, Boolean)

ResourceWriter.AddResource Method (String, Stream, Boolean)

.NET Framework (current version)

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.

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

public void AddResource(
	string name,
	Stream value,
	bool closeAfterWrite


Type: System.String

The name of the resource to add.

Type: System.IO.Stream

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

Type: System.Boolean

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

Exception Condition

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


The stream does not support the Stream.Length property.


name or value is null.


This ResourceWriter has been closed.

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.

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.

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.

.NET Framework
Available since 4.0
Return to top
© 2016 Microsoft