實例講解.NET中資源文件的創建與使用
2024-07-10 12:59:04
供稿:網友
實例講解.net中資源文件的創建與使用
一、資源文件
資源文件顧名思義就是存放資源的文件。資源文件在程序設計中有著自身獨特的優勢,他獨立于源程序,這樣資源文件就可以被多個程序使用。同時在程序設計的時候,有時出于安全或者其他方面因素的考慮,把重要東西存放在資源文件中,也可以達到保密、安全的效果。那么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");
}
}
}
菜鳥學堂: