C#使用Resources資源文件
一、創(chuàng)建資源文件
可以將字符串、圖像或?qū)ο髷?shù)據(jù)等資源包含在資源文件中,方便應(yīng)用程序使用。
創(chuàng)建資源文件的方法:
1、手動或使用IDE工具自動生成XML 資源 (.resx) 文件。(推薦)
創(chuàng)建一個包含字符串、圖像或?qū)ο髷?shù)據(jù)的 XML 資源 (.resx) 文件。
使用 Visual Studio 創(chuàng)建一個資源文件并將其包含在項(xiàng)目中。
Visual Studio 提供一個資源編輯器,借助該編輯器,可添加、刪除和修改資源。 編譯時,資源文件會自動轉(zhuǎn)換成二進(jìn)制 .resources 文件,并嵌入應(yīng)用程序程序集或附屬程序集中。
注意:內(nèi)部使用資源文件生成器 (Resgen.exe) 將文本文件轉(zhuǎn)換成二進(jìn)制資源 (.resources) 文件。 然后使用語言編譯器將這個二進(jìn)制資源文件嵌入可執(zhí)行應(yīng)用程序或應(yīng)用程序庫,或者使用程序集鏈接器 (Al.exe) 將這個二進(jìn)制資源文件嵌入附屬程序集。
2、以編程方式創(chuàng)建一個 XML 資源 (.resx) 文件。 使用ResXResourceWriter類
可以創(chuàng)建一個 .resx 文件、枚舉其資源并按名稱檢索特定資源。
ResXResourceWriter rw = new ResXResourceWriter("Demo.rex");
rw.AddResource("Logo", Image.FromFile("logo.jpg");
rw.AddResource("Title", "Proce c#");
rw.Generate();
rw.Close();二、使用ResourceManager讀取項(xiàng)目中資源文件
資源文件調(diào)用方法
(1).txt 文件:不可以直接調(diào)用,得先將其轉(zhuǎn)換成 .resources 文件才能使用。
(2).resx 文件:可以用 ResXResourceReader 來做讀取,但是這種方法不直觀,不推薦直接調(diào)用 .resx 文件。正確的方法是將其轉(zhuǎn)換成 .resources 文件,然后用 ResourceManager 作讀取工作。
注意如果是在 VS.NET 中添加的 .resx 文件,那么它們自動被設(shè)為 Embedded Resource,轉(zhuǎn)成 .resources 文件后被嵌入到 Assembly 中。
(3).resources 文件分成兩種情況:
- 1、被嵌入或編譯成 Satellite Assembly:使用ResourceManager來獲得在 Assembly 中的資源。
- 2、單獨(dú)文件,沒被編譯或嵌入到Assembly:使用ResourceManager.CreateFileBasedResourceManager來獲得資源集(ResourceSet)。
在新建的Windows項(xiàng)目中,一般自動生成兩個資源文件:Form1.resx和Resources.resx。其中,F(xiàn)orm1.resx附屬Form1窗體,Resources.resx在項(xiàng)目Properties子文件夾下。
下面我們看看讀取項(xiàng)目中資源文件
1、強(qiáng)類型讀取項(xiàng)目中resx資源文件。
1、創(chuàng)建一個項(xiàng)目的默認(rèn)資源文件。

2、在項(xiàng)目“Properties”目錄下,可以看到Resources.resx。

3、添加相關(guān)的資源。

4、可以看到系統(tǒng)自動生成的強(qiáng)類型類。

使用強(qiáng)類型可直接使用類
logo.Image = ZS.MouldManagement.Properties.Resources._0
不僅在Properties文件夾下可以建立資源文件,也可以在任何地方創(chuàng)建資源文件。在建立名稱為MyResource資源文件的同時,設(shè)計(jì)器會自動生成兩個文件:MyResource.resx和MyResource.Designer.cs。其中后一個文件就包裝了設(shè)計(jì)器自動生成的代碼,以方便對資源文件的強(qiáng)類型訪問。

強(qiáng)類型訪問代碼:
ZS.MouldManagement.Mould._333
2、使用代碼讀取項(xiàng)目中不同文化的resx資源文件
ResourceManager 可以根據(jù)不同的 UICulture 設(shè)置返回不同的本地資源,我們只需知道調(diào)用資源用到它就可以了。
Winform使用資源文件(ZS.MouldManagement.resx;ZS.MouldManagement.en-us.rex等等)
//多語言
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-us");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
Assembly asm = Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("ZS.MouldManagement.Properties.Resources", asm);
//ZS.MouldManagement是你程序的命名空間,ZS.MouldManagement.Properties 是資源類Resources的命名空間, 資源文件名稱不帶擴(kuò)展名
this.logo.Image = (Image)rm.GetObject("Logo");//Get**方法具有重載方法,第二個參數(shù)為CultureInfo對象
this.Title.Text = rm.GetString("Title"); //資源文件名稱不帶擴(kuò)展名三、Windows項(xiàng)目中的窗體資源文件
因?yàn)閷orm1.resx的訪問需要構(gòu)造Syste.Resources.ResourceManager對象,而訪問Resources.resx里的資源可以直接使用Properties.Resources.resName(resName是你添加的資源的名稱)。
只有向窗體上添加圖標(biāo)、圖像等資源后

會在窗體設(shè)計(jì)器Form1.Designer.cs自動生成下面的代碼
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));//創(chuàng)建ResourceManager對象
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));//根據(jù)資源的名稱獲取字符串資源
this.Item1.Image = global::ZS.MouldManagement.Properties.Resources._0; //訪問項(xiàng)目中的資源從代碼可以看出,c#將窗體的圖標(biāo)文件作為資源進(jìn)行保存了起來,然后利用ComponentResourceManager來獲取這些資源。
它和ResourceManager用法一樣,System.ComponentModel.ComponentResourceManager繼承自Syste.Resources.ResourceManager。
使用ComponentResourceManager類進(jìn)行界面多語言切換
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
resources.ApplyResources(this, "$this");
AppLang(form, resources);
#region AppLang for control
/// <summary>
/// 遍歷窗體所有控件,針對其設(shè)置當(dāng)前界面語言
/// </summary>
/// <param name="control"></param>
/// <param name="resources"></param>
private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
{
if (control is MenuStrip)
{
resources.ApplyResources(control, control.Name);
MenuStrip ms = (MenuStrip)control;
if (ms.Items.Count > 0)
{
foreach (ToolStripMenuItem c in ms.Items)
{
AppLang(c, resources);
}
}
}
foreach (Control c in control.Controls)
{
resources.ApplyResources(c, c.Name);
AppLang(c, resources);
}
}
#endregion
#region AppLang for menuitem
/// <summary>
/// 遍歷菜單
/// </summary>
/// <param name="item"></param>
/// <param name="resources"></param>
private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
{
if (item is ToolStripMenuItem)
{
resources.ApplyResources(item, item.Name);
ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
if (tsmi.DropDownItems.Count > 0)
{
foreach (ToolStripMenuItem c in tsmi.DropDownItems)
{
AppLang(c, resources);
}
}
}
}
#endregion四、使用Assembly.GetManifestResourceStream()從此程序集加載指定的文件。
當(dāng)直接嵌入一資源時,也就是說,不通過一個資源文件而直接將一資源(比如圖片)嵌入到 Assembly 中。這可以在 VS.NET 中通過設(shè)置一文件的 Build 屬性為“嵌入的資源” 實(shí)現(xiàn)。
在這種情況下 ResourceManager 就沒有用了,因?yàn)樗荒塬@取 .resources 資源文件。那么如何調(diào)用這類的資源呢?我們只要了解一些 System.Reflection.Assembly 這個類中的一些函數(shù)就可以了。
有三個相關(guān)函數(shù),不過我們只需要 Assembly.GetManifestResourceStream 這個函數(shù)。這個函數(shù)將一嵌入到 Assembly 中的資源以 stream 的方式返回,而我們可以將這個 stream 轉(zhuǎn)成在 .NET 中可用的對象。
比如,如果嵌入資源是一圖片,那么我們可以利用 new Bitmap(Stream) 這個構(gòu)造方法獲得這個圖片資源的 Bitmap 對象。
1、將本地要加入的資源文本(視頻,圖片,文本或其它)加入項(xiàng)目。比如我們現(xiàn)在加入一個up.bmp的圖片到項(xiàng)目中,且放在文件夾Resources下面,
2、將up.bmp右鍵選擇“屬性”的“生成操作”設(shè)置為"嵌入的資源"。
注意:訪問資源的名稱規(guī)則為:項(xiàng)目命名空間.資源文件所在文件夾名.資源文件名
Assembly assm = Assembly.getGetExecutingAssembly();
Stream stream = assm.GetManifestResourceStream("CreateDatabase.Resources.aa.txt");
//讀取流
//文本
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string str = sr.ReadToEnd();
//XML文件
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
//圖片
Image img = Image.FromStream(stream);到此這篇關(guān)于C#使用Resources資源文件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#調(diào)用c++的DLL的實(shí)現(xiàn)方法
本文主要介紹了c#調(diào)用c++的DLL的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C# linq查詢之動態(tài)OrderBy用法實(shí)例
這篇文章主要介紹了C# linq查詢之動態(tài)OrderBy用法,實(shí)例分析了C#采用linq方式查詢時動態(tài)排序的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C# Xamarin利用ZXing.Net.Mobile進(jìn)行掃碼的方法
這篇文章主要介紹了C# Xamarin利用ZXing.Net.Mobile進(jìn)行掃碼的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
c#項(xiàng)目實(shí)現(xiàn)發(fā)布到服務(wù)器全過程
這篇文章主要介紹了c#項(xiàng)目實(shí)現(xiàn)發(fā)布到服務(wù)器全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
C#中Thread(線程)和Task(任務(wù))實(shí)例詳解
.NET Framework在System.Threading命名空間中具有與線程相關(guān)的類,線程是一小組可執(zhí)行指令,這篇文章主要給大家介紹了關(guān)于C#中Thread(線程)和Task(任務(wù))的相關(guān)資料,需要的朋友可以參考下2022-03-03

