ResourceWriter.AddResource 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
向要写入的资源的列表中添加资源。
重载
AddResource(String, Byte[]) |
将指定为字节数组的已命名资源添加到要写入的资源列表中。 |
AddResource(String, Stream) |
将指定的命名资源以流的形式添加到要写入的资源列表中。 |
AddResource(String, Object) |
将指定为对象的已命名资源添加到要写入的资源列表中。 |
AddResource(String, String) |
向要写入的资源的列表中添加字符串资源。 |
AddResource(String, Stream, Boolean) |
将指定的命名资源以流的形式添加到要写入的资源列表中,并指定是否应在调用 Generate() 方法后关闭该流。 |
AddResource(String, Byte[])
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
将指定为字节数组的已命名资源添加到要写入的资源列表中。
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)
- Source:
- ResourceWriter.core.cs
- Source:
- ResourceWriter.core.cs
- Source:
- ResourceWriter.core.cs
将指定的命名资源以流的形式添加到要写入的资源列表中。
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) 方法将已保存到 MemoryStream 对象的 ResourceWriter 图形图像添加到 对象。
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)
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
将指定为对象的已命名资源添加到要写入的资源列表中。
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)
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
向要写入的资源的列表中添加字符串资源。
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)
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
- Source:
- ResourceWriter.cs
将指定的命名资源以流的形式添加到要写入的资源列表中,并指定是否应在调用 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
如果在调用 Generate() 方法后关闭该流,则为 true
;否则为 false
。
例外
name
或 value
为 null
。
示例
以下示例使用 AddResource(String, Stream, Boolean) 方法将已保存到 MemoryStream 对象的 ResourceWriter 图形图像添加到 对象。
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) 方法编写的资源。