一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - 實(shí)例講解.NET中資源文件的創(chuàng)建與使用

實(shí)例講解.NET中資源文件的創(chuàng)建與使用

2019-09-25 10:37asp.net技術(shù)網(wǎng) ASP.NET教程

資源文件顧名思義就是存放資源的文件。資源文件在程序設(shè)計(jì)中有著自身獨(dú)特的優(yōu)勢(shì),他獨(dú)立于源程序,這樣資源文件就可以被多個(gè)程序使用

一、資源文件 
資源文件顧名思義就是存放資源的文件。資源文件在程序設(shè)計(jì)中有著自身獨(dú)特的優(yōu)勢(shì),他獨(dú)立于源程序,這樣資源文件就可以被多個(gè)程序使用。同時(shí)在程序設(shè)計(jì)的時(shí)候,有時(shí)出于安全或者其他方面因素的考慮,把重要東西存放在資源文件中,也可以達(dá)到保密、安全的效果。那么Visual C#所使用的資源文件中到底存放哪些東西呢?在用Visual C#創(chuàng)建資源文件大致可以存放三種類型的數(shù)據(jù)資源,分別是字節(jié)數(shù)組、各種對(duì)象和字符串。本文將結(jié)合一個(gè)程序例子來(lái)具體說(shuō)明用Visual C#是如何創(chuàng)建資源文件的。 
二、創(chuàng)建資源文件所用的類 
在.Net FrameWork SDK中的一個(gè)名字叫System.Resources名稱空間,在此名稱空間中為應(yīng)用程序提供了許多創(chuàng)建、存儲(chǔ)和使用資源文件的類和接口。其中有一個(gè)類叫ResourceWriter,Visual C#就是通過(guò)調(diào)用這個(gè)類來(lái)實(shí)現(xiàn)創(chuàng)建、存儲(chǔ)資源文件的。 
三、創(chuàng)建資源文件的方法 
首先要繼承一個(gè)ResourceWriter類,然后調(diào)用ResourceWriter類的一個(gè)方法Generate ( ),就可以產(chǎn)生一個(gè)資源文件了。具體語(yǔ)句如下: 
ResourceWriter rw = new ResourceWriter ( "My.resources" ) ; 
rw.Generate ( ) ; 
此時(shí)在磁盤的中就會(huì)產(chǎn)生一個(gè)名稱為"My.resources"的資源文件,但此時(shí)的資源文件沒有任何內(nèi)容,下面我們就來(lái)看看如何往資源文件中添加資源。 
四、往資源文件中添加資源的方法 
在ResourceWriter類中提供了一個(gè)AddResource ( )方法,這個(gè)方法的作用就是往資源文件中添加資源的。在Visual C#中對(duì)不同的資源有著不同的加入方式。 
(1).加入字節(jié)數(shù)組,語(yǔ)法格式為: 
public void AddResource ( string , byte [ ] ) ; 
注釋:其中string是在使用資源文件的時(shí)候,此字節(jié)數(shù)組在程序中的的唯一標(biāo)識(shí)符 
(2).加入對(duì)象,語(yǔ)法格式為: 
public void AddResource ( string , object ); 
注釋:其中string是在使用資源文件的時(shí)候,此對(duì)象在程序中的唯一標(biāo)識(shí)符。如: 

復(fù)制代碼代碼如下:


Image image1 = Image.FromFile ("abc1.jpg") ; 
Image image2 = Image.FromFile ( "abc2.jpg" ) ; 
rw.AddResource ( "abc1" , image1 ) ; 
rw.AddResource ( "abc2" , image2 ) ; 


(3).加入字符串,具體語(yǔ)法如下: 
public void AddResource ( string1 , string2) ; 
注釋:其中string1是在使用資源文件的時(shí)候,此字符串在程序中的唯一標(biāo)識(shí)符在本文的程序中,是如此使用的: 
rw.AddResource ( "MyStr" , "從資源文件中讀取字符串!" ); 
至此我們已經(jīng)創(chuàng)建了一個(gè)資源文件,并且在資源文件中加入了若干個(gè)資源,當(dāng)然在這之后,還應(yīng)該注意,保存此資源文件,并關(guān)閉資源文件,具體如下: 
rw.Close ( ) ; 
五、示例創(chuàng)建資源文件 
在這里創(chuàng)建一個(gè)什么樣的工程好呢?有些朋友可能會(huì)用.net創(chuàng)建一個(gè)“控制臺(tái)應(yīng)用程序”,其實(shí)沒必要,直接用記事本創(chuàng)建一個(gè)CS文件就可以了,假如名稱為:CreatResources.cs。編譯命令:csc CreatResources.cs;運(yùn)行:CreatResources。這時(shí)會(huì)產(chǎn)生一個(gè)叫做My.resources的資源文件。先放到這里,等下再用。 

復(fù)制代碼代碼如下:


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


六、將生成的資源文件添加測(cè)試工程中的方法 
創(chuàng)建一個(gè)“Windows應(yīng)用程序”測(cè)試工程,將My.resources資源文件添加到該工程中,步驟如下: 
1,在資源管理器里選中文件 
2,按住鼠標(biāo)左鍵,拖到工程文件上,松開鼠標(biāo)左鍵。 
3,在拖放的文件上點(diǎn)鼠標(biāo)右鍵,選“屬性” 
4,在生成操作里選擇“嵌入的資源”。 
七、如何在程序中管理資源文件中的資源 
在.Net FrameWork SDK這提供了一個(gè)關(guān)于資源文件創(chuàng)建和使用的名稱空間--System.Resources。在這個(gè)名稱空間中有一個(gè)Class為ResourceManager,這個(gè)Class的主要作用就是管理并使用資源文件。Visual C#是通過(guò)這個(gè)類來(lái)管理并使用嵌入程序中的資源文件中的資源。下列代碼就是定義一個(gè)ResourceManager類來(lái)管理嵌入程序資源文件中的資源: 
ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ; 
注意:ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ;語(yǔ)句中,構(gòu)造函數(shù)的第一個(gè)參數(shù)Res.My 由兩部分構(gòu)成,Res表示測(cè)試工程的命名空間,My表示資源文件名My.resources的根名稱,即點(diǎn)號(hào)有前部分My。 
八、如何在程序中使用資源文件中的資源 
使用了AddResource ( )方法來(lái)加入資源,他的語(yǔ)法中的第一個(gè)參數(shù)是用戶可以定義的字符串,這個(gè)字符串就是資源在資源文件的唯一標(biāo)識(shí),在程序設(shè)計(jì)中,就是通過(guò)這個(gè)唯一標(biāo)識(shí)符來(lái)使用某個(gè)資源的。那么如何在程序中通過(guò)這個(gè)標(biāo)識(shí)符來(lái)得到所需資源?這就要使用到ResourceManager類中的GetObject()和GetString()方法。這二個(gè)方法作用是獲得指定的資源。下面是這二個(gè)方法的語(yǔ)法: 
object GetSting(String) 
object GetObject(String) 
其中的"String"就是資源在資源文件中的那個(gè)唯一標(biāo)識(shí)符。細(xì)心的讀者可能已經(jīng)注意到,這二個(gè)方法的返回值都是一個(gè)Object類型的變量,也就是一個(gè)參考類型的變量,而在程序中的字符串或者圖象等,是一個(gè)實(shí)值類型變量。這就需要進(jìn)行轉(zhuǎn)換,而這種轉(zhuǎn)換就是上面所說(shuō)的裝箱和出箱。下列代碼是從資源文件中提取字符串、圖象和圖標(biāo)資源: 
提取字符串資源: 
String s = ( ( String ) rm.GetString ( "MyStr" ) ) ; 
提取圖標(biāo)資源: 
Icon icoDemo = ( ( Icon ) rm.GetObject ( "demo.ico" ) ) ; 
提取圖象資源: 
Image a = ( ( Image ) ( rm.GetObject ( "ok-off.png" ) ) ) ; 
九、測(cè)試工程源代碼 

復(fù)制代碼代碼如下:


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
// 加入以下兩個(gè)命名空間。 
using System.Resources; 
using System.Reflection; 
namespace Res 

/// 
/// Form1 的摘要說(shuō)明。 
/// 
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; 
/// 
/// 必需的設(shè)計(jì)器變量。 
/// 
private System.ComponentModel.Container components = null; 
public Form1() 

// 
// Windows 窗體設(shè)計(jì)器支持所必需的 
// 
InitializeComponent(); 
// 
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼 
// 

/// 
/// 清理所有正在使用的資源。 
/// 
protected override void Dispose( bool disposing ) 

if( disposing ) 

if (components != null) 

components.Dispose(); 


base.Dispose( disposing ); 

#region Windows 窗體設(shè)計(jì)器生成的代碼 
/// 
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改 
/// 此方法的內(nèi)容。 
/// 
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 = "圖標(biāo)"; 
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 
/// 
/// 應(yīng)用程序的主入口點(diǎn)。 
/// 
[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"); 


延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本高h | 9久热这里只有精品视频在线观看 | 亚洲精品资源在线 | 肉性天堂| 欧美5g影院 | 国产精品成人亚洲 | 久久精品视频uu | 国产精品性视频免费播放 | 九色PORNY真实丨国产大胸 | 色综合久久中文字幕网 | 欧美日韩视频在线第一区二区三区 | 日韩精品欧美高清区 | 亚洲性69影视 | 挺进白嫩老师下面视频 | 亚洲国产成人久久综合区 | 亚洲 日韩 国产 制服 在线 | 99re这里只有精品视频在线观看 | 日韩精品亚洲专区在线影视 | 顶级尤物极品女神福利视频 | 黑人巨大vs北条麻妃在线 | 国产欧美日韩精品高清二区综合区 | 亚洲白拍 | 精品一区久久 | 国产精品高清在线 | 91热爆在线 | 亚洲国产免费观看视频 | sese在线播放 | 日韩一区二区在线视频 | 欧美日韩1区2区 | 国产麻豆精品视频 | 成人aaaa | 麻豆网站视频国产在线观看 | 消息称老熟妇乱视频一区二区 | 99国产小视频 | 色婷婷天天综合在线 | yellow视频在线观看 | 欧美日本一道高清免费3区 欧美人做人爱a全程免费 | 精品卡1卡2卡三卡免费视频 | 日本三级香港三级久久99 | 色综合久久98天天综合 | asian4you裸模 |