ResourceWriter.AddResource 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將資源加入要寫入的資源清單。
多載
AddResource(String, Byte[]) |
將指定為位元組陣列的具名資源加入要寫入的資源清單。 |
AddResource(String, Stream) |
加入具名資源,這個資源已指定為要寫入資源清單的資料流。 |
AddResource(String, Object) |
將指定為物件的具名資源加入要寫入的資源清單。 |
AddResource(String, String) |
將字串資源加入至要寫入的資源清單。 |
AddResource(String, Stream, Boolean) |
加入已指定為要寫入資源清單之資料流的具名資源,並指定該資料流是否應於 Generate() 方法呼叫之後關閉。 |
AddResource(String, Byte[])
將指定為位元組陣列的具名資源加入要寫入的資源清單。
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())
參數
- name
- String
資源名稱。
- value
- Byte[]
資源的值,為 8 位元不帶正負號的整數陣列。
實作
例外狀況
name
(或者僅有大寫差別的名稱) 已經加入至這個 ResourceWriter。
name
參數為 null
。
這個 ResourceWriter 已經關閉,而且其雜湊表無法使用。
範例
下列範例會 AddResource(String, Byte[]) 使用 方法,將已讀取為位元組陣列的圖形影像新增至 ResourceWriter 物件。
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
備註
在呼叫 之前 Generate ,不會寫入資源。
您可以藉由呼叫 ResourceManager.GetStream 方法來擷取 方法所AddResource(String, Byte[])撰寫的資源。
另請參閱
適用於
AddResource(String, Stream)
加入具名資源,這個資源已指定為要寫入資源清單的資料流。
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)
參數
- name
- String
要加入的資源名稱。
例外狀況
name
或 value
為 null
。
範例
下列範例會 AddResource(String, Stream) 使用 方法,將已儲存至 物件的圖形影像新增至 MemoryStreamResourceWriter 物件。
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
備註
您可以指定支援 Stream.Length 屬性 value
的任何資料流。
您可以藉由呼叫 ResourceManager.GetStream 方法來擷取 方法所AddResource(String, Stream)撰寫的資源。
另請參閱
適用於
AddResource(String, Object)
將指定為物件的具名資源加入要寫入的資源清單。
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)
參數
- name
- String
資源名稱。
- value
- Object
資源的值。
實作
例外狀況
name
(或者僅有大寫差別的名稱) 已經加入至這個 ResourceWriter。
name
參數為 null
。
這個 ResourceWriter 已經關閉,而且其雜湊表無法使用。
範例
下列範例會使用 方法, AddResource(String, Object) 將對象數據新增至二進位資源檔。
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
是自定義類別,其欄位包含要顯示在使用者介面中的數據行名稱。 請注意,類別會以 SerializableAttribute 屬性標示。 一般而言,類別會在個別元件中定義,並在編譯時期提供給編譯程序的參考。
備註
value
必須可序列化。
在呼叫 方法之前 Generate ,不會寫入資源。
您可以藉由呼叫 ResourceManager.GetObject 方法來擷取 方法所AddResource(String, Object)撰寫的資源。
另請參閱
適用於
AddResource(String, String)
將字串資源加入至要寫入的資源清單。
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)
參數
- name
- String
資源名稱。
- value
- String
資源的值。
實作
例外狀況
name
(或者僅有大寫差別的名稱) 已經加入至這個 ResourceWriter。
name
參數為 null
。
這個 ResourceWriter 已經關閉,而且其雜湊表無法使用。
範例
下列範例會使用 方法, AddResource 將字串資源新增至 ResourceWriter 物件。
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
備註
在呼叫 之前 Generate ,不會寫入資源。
您可以藉由呼叫 ResourceManager.GetString 方法來擷取 方法所AddResource(String, String)撰寫的資源。
另請參閱
適用於
AddResource(String, Stream, Boolean)
加入已指定為要寫入資源清單之資料流的具名資源,並指定該資料流是否應於 Generate() 方法呼叫之後關閉。
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)
參數
- name
- String
要加入的資源名稱。
- closeAfterWrite
- Boolean
true
表示要在呼叫 Generate() 方法後關閉資料流,否則為 false
。
例外狀況
name
或 value
為 null
。
範例
下列範例會 AddResource(String, Stream, Boolean) 使用 方法,將已儲存至 物件的圖形影像新增至 MemoryStreamResourceWriter 物件。
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
備註
您可以指定支援 Stream.Length 屬性 value
的任何資料流。
您可以藉由呼叫 ResourceManager.GetStream 方法來擷取 方法所AddResource(String, Stream, Boolean)撰寫的資源。