ResourceWriter.AddResource Metoda

Definicja

Dodaje zasób do listy zasobów do zapisania.

Przeciążenia

AddResource(String, Byte[])

Dodaje nazwany zasób określony jako tablica bajtów do listy zasobów do zapisania.

AddResource(String, Stream)

Dodaje nazwany zasób określony jako strumień do listy zasobów do zapisania.

AddResource(String, Object)

Dodaje nazwany zasób określony jako obiekt do listy zasobów do zapisania.

AddResource(String, String)

Dodaje zasób ciągu do listy zasobów do zapisania.

AddResource(String, Stream, Boolean)

Dodaje nazwany zasób określony jako strumień do listy zasobów do zapisania i określa, czy strumień powinien zostać zamknięty po wywołaniu Generate() metody.

AddResource(String, Byte[])

Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs

Dodaje nazwany zasób określony jako tablica bajtów do listy zasobów do zapisania.

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())

Parametry

name
String

Nazwa zasobu.

value
Byte[]

Wartość zasobu jako 8-bitowej tablicy niepodpisanej liczby całkowitej.

Implementuje

Wyjątki

name (lub nazwa, która różni się tylko przez wielkość liter) została już dodana do tego ResourceWriterelementu .

Parametr name ma wartość null.

Ta ResourceWriter funkcja została zamknięta i jej tabela skrótów jest niedostępna.

Przykłady

W poniższym przykładzie AddResource(String, Byte[]) użyto metody w celu dodania obrazu graficznego, który został odczytany jako tablica ResourceWriter bajtów do obiektu.

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

Uwagi

Zasób nie jest zapisywany, dopóki Generate nie zostanie wywołany.

Zasoby zapisywane przez metodę AddResource(String, Byte[]) można pobrać, wywołując metodę ResourceManager.GetStream .

Zobacz też

Dotyczy

AddResource(String, Stream)

Źródło:
ResourceWriter.core.cs
Źródło:
ResourceWriter.core.cs
Źródło:
ResourceWriter.core.cs

Dodaje nazwany zasób określony jako strumień do listy zasobów do zapisania.

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)

Parametry

name
String

Nazwa zasobu do dodania.

value
Stream

Wartość zasobu do dodania. Zasób musi obsługiwać Length właściwość .

Wyjątki

name (lub nazwa, która różni się tylko przez wielkość liter) została już dodana do tego ResourceWriterelementu .

-lub-

Strumień nie obsługuje Length właściwości .

name lub value ma wartość null.

Zostało to ResourceWriter zamknięte.

Przykłady

W poniższym przykładzie AddResource(String, Stream) użyto metody w celu dodania obrazu graficznego MemoryStream , który został zapisany w obiekcie do ResourceWriter obiektu.

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

Uwagi

Można określić dowolny strumień obsługujący Stream.Length właściwość dla elementu value.

Zasoby zapisywane przez metodę AddResource(String, Stream) można pobrać, wywołując metodę ResourceManager.GetStream .

Zobacz też

Dotyczy

AddResource(String, Object)

Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs

Dodaje nazwany zasób określony jako obiekt do listy zasobów do zapisania.

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)

Parametry

name
String

Nazwa zasobu.

value
Object

Wartość zasobu.

Implementuje

Wyjątki

name (lub nazwa, która różni się tylko przez wielkość liter) została już dodana do tego ResourceWriterelementu .

Parametr name ma wartość null.

Ta ResourceWriter funkcja została zamknięta i jej tabela skrótów jest niedostępna.

Przykłady

W poniższym przykładzie użyto AddResource(String, Object) metody w celu dodania danych obiektu do pliku zasobów binarnych.

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 jest klasą niestandardową, której pola zawierają nazwy kolumn, które mają być wyświetlane w interfejsie użytkownika. Należy pamiętać, że klasa jest oznaczona atrybutem SerializableAttribute . Zwykle klasa będzie definiowana w osobnym zestawie, a odwołanie do niego zostanie udostępnione kompilatorowi w czasie kompilacji.

Uwagi

value musi być serializowalny.

Zasób nie jest zapisywany do momentu wywołania Generate metody .

Zasoby zapisywane przez metodę AddResource(String, Object) można pobrać, wywołując metodę ResourceManager.GetObject .

Zobacz też

Dotyczy

AddResource(String, String)

Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs

Dodaje zasób ciągu do listy zasobów do zapisania.

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)

Parametry

name
String

Nazwa zasobu.

value
String

Wartość zasobu.

Implementuje

Wyjątki

name (lub nazwa, która różni się tylko przez wielkość liter) została już dodana do tego elementu ResourceWriter.

Parametr name ma wartość null.

Ta ResourceWriter funkcja została zamknięta i jej tabela skrótów jest niedostępna.

Przykłady

W poniższym przykładzie użyto AddResource metody , aby dodać zasoby ciągów do ResourceWriter obiektu.

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

Uwagi

Zasób nie jest zapisywany, dopóki Generate nie zostanie wywołany.

Zasoby zapisywane przez metodę AddResource(String, String) można pobrać, wywołując metodę ResourceManager.GetString .

Zobacz też

Dotyczy

AddResource(String, Stream, Boolean)

Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs
Źródło:
ResourceWriter.cs

Dodaje nazwany zasób określony jako strumień do listy zasobów do zapisania i określa, czy strumień powinien zostać zamknięty po wywołaniu Generate() metody.

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)

Parametry

name
String

Nazwa zasobu do dodania.

value
Stream

Wartość zasobu do dodania. Zasób musi obsługiwać Length właściwość .

closeAfterWrite
Boolean

true aby zamknąć strumień po wywołaniu Generate() metody ; w przeciwnym razie false.

Wyjątki

name (lub nazwa, która różni się tylko przez wielkość liter) została już dodana do tego ResourceWriterelementu .

-lub-

Strumień nie obsługuje Length właściwości .

name lub value ma wartość null.

Zostało to ResourceWriter zamknięte.

Przykłady

W poniższym przykładzie AddResource(String, Stream, Boolean) użyto metody w celu dodania obrazu graficznego MemoryStream , który został zapisany w obiekcie do ResourceWriter obiektu.

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

Uwagi

Można określić dowolny strumień obsługujący Stream.Length właściwość dla elementu value.

Zasoby zapisywane przez metodę AddResource(String, Stream, Boolean) można pobrać, wywołując metodę ResourceManager.GetStream .

Zobacz też

Dotyczy