如何使用資源檔案

2021-08-22 08:57:59 字數 3420 閱讀 3375

昨天突發奇想,向寫乙個類似於msn 的自定義個性化窗體,今天在實踐的過程中,發現在實現資源檔案時就遇到了困難。

在網上找了不少的資料,將怎麼新增資源檔案的很多,比如這個

就不錯,而且其中也註解了:

注意:resourcemanager rm = new resourcemanager ( "res.my

" , assembly.getexecutingassembly ( ) ) ;語句中,建構函式的第乙個引數res.my

由兩部分構成,res表示測試工程的命名空間,my表示資源檔名my.resources的根名稱,即點號有前部分my

然而自己在實踐過程中,還是對basename 的理解發生乙個錯誤。由於自己使用c#寫程式的,剛開始認為新增資源檔案很簡單,就直接用resourcemanager區讀取.resouce檔案使用,但是失敗了。

//private const system.string resource_base_name = "component.popup";

this.resourcedirectory = @"e:/projects/popup/src/resources";

system.threading.thread.currentthread.currentuiculture = new system.globalization.cultureinfo("zh-cn");

this.resmng = system.resources.resourcemanager.createfilebasedresourcemanager(resource_base_name, this.resourcedirectory, typeof(popupctl));

隨後又換成resourcewriter 來自己寫.resource 檔案,呼叫還是出現了問題。

this.resourcedirectory = @"e:/projects/popup/src/resources";

system.threading.thread.currentthread.currentuiculture = new system.globalization.cultureinfo("zh-cn");

this.createresource();

this.resmng = new system.resources.resourcemanager("component.popup.resources", system.reflection.assembly.getexecutingassembly());

image img = (system.drawing.image)this.resmng.getobject("btn_close") //這句報錯,說是找不到btn_close

private void createresource()

不知道自己是在那裡錯誤了,找了1個小時的網上資料也沒有結果。剛好電視在放《唐伯虎點秋香》,哈哈,又看到了「剛出爐的孝子大拍賣」,「美女」,「這是你們逼我的——龜波氣功」。哈哈,經典就是經典。

看完了電視再來看看程式,無聊的翻看著程式所在的目錄發現在obj/debug目錄下有這個乙個檔案component.popup.properties.resources.resouce

看著這個檔案怎麼這麼熟悉,好像是資源檔案,而且檔名中多了乙個properties ,這個不是vs2005中的屬性目錄嗎? 難道說是程式自動把資源檔案所在目錄也加入到了名稱中?

返回程式,修改了basename 的字串為 component.popup.properties.resources ,執行程式,一切ok。

暈,怎麼會這樣?到底是怎麼回事?馬上上microsfot查詢,找到了這片文章:

其中有這麼一段:

如何準確的定義資源檔案的邏輯位置

我想這是許多人最關注的一段了!在這裡作者將解說如何正確的填寫 resoucemanager(string, assembly) 這個 constructor,還有如何正確的填寫 assembly.getmanifestresourcestream(string),因為它們兩個的原理是相同的。看過了上面的描述,到了這裡就簡單多了。這裡主要討論的是怎麼填寫那個 string。這個 string 就是資源的完整名,乙個完整名由它的名字空間和檔名前部分(basename)組成。例如,如果預設名字空間(root namespace)是 defaultnamespace,資源檔案的名字是 strings.en-us.resources,那麼它的完整名就是 defaultnamespace.strings。這個很簡單,不過怎樣確定名字空間呢?這就有些奇怪了,因為 c# 的編譯器與 vb.net 的編譯器有些不同。作者在這裡分別給出兩個編譯器怎樣給嵌入資源自動新增命名空間:c#

它自動新增 default namespace(與 root namespace 相同),但也新增子資料夾的名字。例如,在 subfolder 子資料夾下放的資源檔案 strings.en-us.resources,它的完整名是 default namespace + subfolder + base name = defaultnamespace.subfolder.strings

vb.net

在 vb.net 中就很簡單了,它自動給嵌入資源新增 root namespace。不管你在哪個子資料夾中放置資源檔案,資源檔案的完整名永遠是 root namespace + base name。

根據上面的描述,如果我們使用 c#,用 vs.net 在 newfolder 這個子資料夾中新增了乙個叫 images.resources 的資源檔案,那麼我們應該用以下**獲取這些資源,假設 default namespace 是 mydefault:

resourcemanager res = new resourcemanager("mydefault.newfolder.images", this.gettype().assembly);

但如果我們用 vb.net 的話,就應該這樣:

dim res as new resourcemanager("mydefault.images", me.gettype().assembly)

看完上面的解釋才發現自己的正確性,再更改createresource,執行,ok。一路綠燈。哈哈,成功了。

總結一下:

1、不要眼高手低。雖然看到資源檔案很簡單,大體上你可能知道,但是不妨動手試一下,也許你遇到乙個強有力的對手的;

2、對於困難的問題可以適當休息後再解決;

3、《唐伯虎點秋香》經典還是經典。

image img = (system.drawing.image)this.resmng.getobject("btn_close") //這句報錯,說是找不到btn_close

如何使用Resource資源檔案

為了能夠在displayattribute中重複使用同樣的名稱,將所有的顯示字串集中管理。1 displayattribute本身支援直接使用資源檔案。display resourcetype typeof resource2 name studentname resourcetype為資源檔名,表...

如何生成資源檔案

1.建立乙個資源檔案 rc,類似以下內容 define createprocess manifest resource id 1 define rt manifest 24 define control panel resource id 123 createprocess manifest res...

資源檔案的使用

學習pyqt推薦大家看這本書 第一步 new file qt resource file 第二步 add prefix建乙個字首 prefix 相當於建立乙個資料夾嘛,可以分類圖示。新增具體圖示就是add files就可以了。這裡的images這個子資料夾,你在命名的加上 images就可以了。su...