Bewerken

Share via


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.

public:
 virtual void AddResource(System::String ^ name, cli::array <System::Byte> ^ value);
public void AddResource (string name, byte[]? value);
public void AddResource (string name, byte[] value);
abstract member AddResource : string * byte[] -> unit
override this.AddResource : string * byte[] -> unit
Public Sub AddResource (name As String, value As Byte())

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.

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.
      }
   }
}
Imports System.IO
Imports System.Resources

Module Example
   Public Sub Main()                      
      ' Get the image as an array of bytes.
      Dim byteStream As New FileStream("AppIcon.jpg", Filemode.Open)
      Dim bytes(CInt(byteStream.Length - 1)) As Byte
      byteStream.Read(bytes, 0, CInt(byteStream.Length))
      
      ' Create the resource file.
      Using rw As New ResourceWriter(".\UIImages.resources")
         rw.AddResource("AppIcon", byteStream)
         ' Add any other resources.
      End Using
   End Sub
End Module

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

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.

public:
 void AddResource(System::String ^ name, System::IO::Stream ^ value);
public void AddResource (string name, System.IO.Stream? value);
public void AddResource (string name, System.IO.Stream value);
member this.AddResource : string * System.IO.Stream -> unit
Public Sub AddResource (name As String, value As Stream)

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.

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.
      }
   }
}
Imports System.Drawing
Imports System.IO
Imports System.Resources

Module Example
   Public Sub Main()
      ' Bitmap as stream
      Dim bitmapStream As New MemoryStream()
      Dim bmp As New Bitmap(".\\AppImage.jpg")
      bmp.Save(bitmapStream, Imaging.ImageFormat.Jpeg)
          
      Using rw As New ResourceWriter(".\UIImages.resources")
         rw.AddResource("Bitmap", bitmapStream)
         ' Add other resources.
      End Using
   End Sub
End Module

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

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.

public:
 virtual void AddResource(System::String ^ name, System::Object ^ value);
public void AddResource (string name, object? value);
public void AddResource (string name, object value);
abstract member AddResource : string * obj -> unit
override this.AddResource : string * obj -> unit
Public Sub AddResource (name As String, value As Object)

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.

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

AddResource(String, String)

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

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

public:
 virtual void AddResource(System::String ^ name, System::String ^ value);
public:
 void AddResource(System::String ^ name, System::String ^ value);
public void AddResource (string name, string? value);
public void AddResource (string name, string value);
abstract member AddResource : string * string -> unit
override this.AddResource : string * string -> unit
member this.AddResource : string * string -> unit
Public Sub AddResource (name As String, value As String)

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.

using namespace System;
using namespace System::Resources;
using namespace System::IO;
int main()
{
   
   // Create a file stream to encapsulate items.resources.
   FileStream^ fs = gcnew FileStream( "items.resources",FileMode::OpenOrCreate,FileAccess::Write );
   
   // Open a resource writer to write from the stream.
   IResourceWriter^ writer = gcnew 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();
}
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();
    }
}
Imports System.Resources
Imports System.IO

Public Class WriteResources
   
    Public Shared Sub Main(args() As String)
        ' Create a file stream to encapsulate items.resources.
        Dim fs As New FileStream("items.resources", _
           FileMode.OpenOrCreate, FileAccess.Write)
      
        ' Open a resource writer to write from the stream.
        Dim 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()
    End Sub

End Class

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

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.

public:
 void AddResource(System::String ^ name, System::IO::Stream ^ value, bool closeAfterWrite);
public void AddResource (string name, System.IO.Stream? value, bool closeAfterWrite = false);
public void AddResource (string name, System.IO.Stream value, bool closeAfterWrite);
member this.AddResource : string * System.IO.Stream * bool -> unit
Public Sub AddResource (name As String, value As Stream, Optional closeAfterWrite As Boolean = false)
Public Sub AddResource (name As String, value As Stream, closeAfterWrite As Boolean)

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.

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();
   }
}
Imports System.Drawing
Imports System.IO
Imports System.Resources

Module Example
   Public Sub Main()
      ' Bitmap as stream
      Dim bitmapStream As New MemoryStream()
      Dim bmp As New Bitmap(".\\AppImage.jpg")
      bmp.Save(bitmapStream, Imaging.ImageFormat.Jpeg)
          
      Dim rw As New ResourceWriter(".\UIImages.resources")
      rw.AddResource("Bitmap", bitmapStream, True)
      ' Add other resources.
      rw.Generate()
   End Sub
End Module

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