一、資源檔
資源檔顧名思義就是存放資源的檔案。資源檔在程式設計中有著自身獨特的優勢,他獨立於來源程式,這樣資源檔就可以被多個程式使用。同時在程式設計的時候,有時出於安全或者其他方面因素的考慮,把重要東西存放在資源檔中,也可以達到保密、安全的效果。那麼Visual C#所使用的資源檔中到底存放哪些東西呢?在用Visual C#建立資源檔大致可以存放三種類型的資料資源,分別是位元組數組、各種對象和字串。本文將結合一個程式例子來具體說明用Visual C#是如何建立資源檔的。
二、建立資源檔所用的類
在.Net FrameWork SDK中的一個名字叫System.Resources名稱空間,在此名稱空間中為應用程式提供了許多建立、儲存和使用資源檔的類和介面。其中有一個類叫ResourceWriter,Visual C#就是通過調用這個類來實現建立、儲存資源檔的。
三、建立資源檔的方法
首先要繼承一個ResourceWriter類,然後調用ResourceWriter類的一個方法Generate ( ),就可以產生一個資源檔了。具體語句如下:
ResourceWriter rw = new ResourceWriter ( "My.resources" ) ;
rw.Generate ( ) ;
此時在磁碟的中就會產生一個名稱為"My.resources"的資源檔,但此時的資源檔沒有任何內容,下面我們就來看看如何往資源檔中添加資源。
四、往資源檔中添加資源的方法
在ResourceWriter類中提供了一個AddResource ( )方法,這個方法的作用就是往資源檔中添加資源的。在Visual C#中對不同的資源有著不同的加入方式。
(1).加入位元組數組,文法格式為:
public void AddResource ( string , byte [ ] ) ;
注釋:其中string是在使用資源檔的時候,此位元組數組在程式中的的唯一識別碼
(2).加入對象,文法格式為:
public void AddResource ( string , object );
注釋:其中string是在使用資源檔的時候,此對象在程式中的唯一識別碼。如:
複製代碼 代碼如下:
Image image1 = Image.FromFile ("abc1.jpg") ;
Image image2 = Image.FromFile ( "abc2.jpg" ) ;
rw.AddResource ( "abc1" , image1 ) ;
rw.AddResource ( "abc2" , image2 ) ;
(3).加入字串,具體文法如下:
public void AddResource ( string1 , string2) ;
注釋:其中string1是在使用資源檔的時候,此字串在程式中的唯一識別碼在本文的程式中,是如此使用的:
rw.AddResource ( "MyStr" , "從資源檔中讀取字串!" );
至此我們已經建立了一個資源檔,並且在資源檔中加入了若干個資源,當然在這之後,還應該注意,儲存此資源檔,並關閉資源檔,具體如下:
rw.Close ( ) ;
五、樣本建立資源檔
在這裡建立一個什麼樣的工程好呢?有些朋友可能會用.net建立一個“控制台應用程式”,其實沒必要,直接用記事本建立一個CS檔案就可以了,假如名稱為:CreatResources.cs。編譯命令:csc CreatResources.cs;運行:CreatResources。這時會產生一個叫做My.resources的資源檔。先放到這裡,等下再用。
複製代碼 代碼如下:
using System ;
using System.Drawing ;
using System.Resources ;
class CreatResource
{
public static void Main ( )
{
ResourceWriter rw = new ResourceWriter ( "My.resources" ) ;
Image image1 = Image.FromFile ("abc1.jpg") ;
Image image2 = Image.FromFile ( "abc2.jpg" ) ;
rw.AddResource ( "abc1" , image1 ) ;
rw.AddResource ( "abc2" , image2 ) ;
Icon ic = new Icon("CDDRIVE.ICO");
rw.AddResource( "abc3",ic);
rw.AddResource( "abc4","這是從資源檔中讀出的字元");
rw.Generate ( ) ;
rw.Close ( ) ;
}
}
六、將產生的資源檔添加測試工程中的方法
建立一個“Windows應用程式”測試工程,將My.resources資源檔添加到該工程中,步驟如下:
1,在資源管理員裡選中檔案
2,按住滑鼠左鍵,拖到工程檔案上,鬆開滑鼠左鍵。
3,在拖放的檔案上點滑鼠右鍵,選“屬性”
4,在產生操作裡選擇“內嵌資源”。
七、如何在程式中管理資源檔中的資源
在.Net FrameWork SDK這提供了一個關於資源檔建立和使用的名稱空間--System.Resources。在這個名稱空間中有一個Class為ResourceManager,這個Class的主要作用就是管理並使用資源檔。Visual C#是通過這個類來管理並使用嵌入程式中的資源檔中的資源。下列代碼就是定義一個ResourceManager類來管理嵌入程式資源檔中的資源:
ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ;
注意:ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ;語句中,建構函式的第一個參數Res.My 由兩部分構成,Res表示測試工程的命名空間,My表示資源檔名My.resources的根名稱,即點號有前部分My。
八、如何在程式中使用資源檔中的資源
使用了AddResource ( )方法來加入資源,他的文法中的第一個參數是使用者可以定義的字串,這個字串就是資源在資源檔的唯一標識,在程式設計中,就是通過這個唯一識別碼來使用某個資源的。那麼如何在程式中通過這個標識符來得到所需資源?這就要使用到ResourceManager類中的GetObject()和GetString()方法。這二個方法作用是獲得指定的資源。下面是這二個方法的文法:
object GetSting(String)
object GetObject(String)
其中的"String"就是資源在資源檔中的那個唯一識別碼。細心的讀者可能已經注意到,這二個方法的傳回值都是一個Object類型的變數,也就是一個參考類型的變數,而在程式中的字串或者圖象等,是一個實實值型別變數。這就需要進行轉換,而這種轉換就是上面所說的裝箱和出箱。下列代碼是從資源檔中提取字串、圖象和表徵圖資源:
提取字串資源:
String s = ( ( String ) rm.GetString ( "MyStr" ) ) ;
提取表徵圖資源:
Icon icoDemo = ( ( Icon ) rm.GetObject ( "demo.ico" ) ) ;
提取圖象資源:
Image a = ( ( Image ) ( rm.GetObject ( "ok-off.png" ) ) ) ;
九、測試工程原始碼
複製代碼 代碼如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
// 加入以下兩個命名空間。
using System.Resources;
using System.Reflection;
namespace Res
{
///
/// Form1 的摘要說明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button4;
///
/// 必需的設計器變數。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
///
/// 清理所有正在使用的資源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
///
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
///
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Location = new System.Drawing.Point(8, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(256, 240);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(280, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 1;
this.button1.Text = "映像1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(280, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.TabIndex = 2;
this.button2.Text = "映像2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(280, 128);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.TabIndex = 2;
this.button3.Text = "文字";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 264);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(360, 16);
this.label1.TabIndex = 4;
this.label1.Text = "label1";
//
// button4
//
this.button4.Location = new System.Drawing.Point(280, 88);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.TabIndex = 2;
this.button4.Text = "表徵圖";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 285);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button4);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
///
/// 應用程式的主進入點。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());
this.pictureBox1.Image = (Bitmap)rm.GetObject("abc1");
}
private void button2_Click(object sender, System.EventArgs e)
{
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());
this.pictureBox1.Image = (Bitmap)rm.GetObject("abc2");
}
private void button4_Click(object sender, System.EventArgs e)
{
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());
this.Icon = (Icon)rm.GetObject("abc3");
}
private void button3_Click(object sender, System.EventArgs e)
{
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());
this.label1.Text = rm.GetString("abc4");
}
}
}