posts - 134,comments - 22,trackbacks - 0

          可以使用 ResourceManager 類在運(yùn)行時檢索“嵌入的資源”(即已經(jīng)編譯到應(yīng)用程序或類庫中的資源)。ResourceManager 類的每個實(shí)例都與一個程序集關(guān)聯(lián)并且管理對嵌入到該程序集中的資源的檢索。

          檢索資源

          1. 創(chuàng)建一個程序集引用,引用包含要訪問的資源的程序集。如果尚未加載包含資源的程序集,則必須在此時加載它。
          2. 創(chuàng)建 ResourceManager 類的實(shí)例以檢索資源。
          3. 指定嵌入文件的基名稱并提供對包含資源的程序集的引用。
            注意   嵌入的資源文件的基名稱是嵌入了資源文件的命名空間的名稱加上不帶任何擴(kuò)展名的文件名。例如,myApplication 命名空間中名為 Resource1.resX 的文件的基名稱是 myApplication.Resource1。
          4. 調(diào)用 ResourceManager.GetString 方法ResourceManager.GetObject 方法以檢索資源。
            • 若要檢索“字符串”資源,請調(diào)用 GetString 方法。
            • 若要檢索其他類型的資源,請調(diào)用 GetObject 方法。通過 GetObject 方法檢索到的資源必須顯式轉(zhuǎn)換成對應(yīng)的類型。

           demo1:

          // C#
          // Gets reference to the same assembly that 
          // contains the type that is creating the ResourceManager.
          System.Reflection.Assembly myAssembly;
          MyAssembly 
          = this.GetType().Assembly;

          // Gets reference to different assembly.
          System.Reflection.Assembly myOtherAssembly;
          myOtherAssembly 
          = System.Reflection.Assembly.Load("ResourceAssembly");

          // Creates the ResourceManager.
          System.Resources.ResourceManager myManager = new 
             System.Resources.ResourceManager(
          "ResourceNamespace.myResources"
             myAssembly);

          // Retrieves String and Image resources.
          System.String myString;
          System.Drawing.Image myImage;
          myString 
          = myManager.GetString("StringResource");
          myImage 
          = (System.Drawing.Image)myManager.GetObject("ImageResource");

           

           

          demo2:

          using System;
          using System.Resources;
          using System.Threading;
          using System.Reflection;
          using System.Globalization;
          class ResourcesExample 
          {
              
          public static void Main() 
              
          {
                  
          // Create resource manager to retrieve resources.
                  ResourceManager rm = new ResourceManager("resourceManager.Application"
                      Assembly.GetExecutingAssembly());

              
          // Get the culture of the currently executing thread.
              
          // The value of ci will determine the culture of
              
          // the resources that the resource manager retrieves.
              CultureInfo ci = Thread.CurrentThread.CurrentCulture;

              
              
          // Retrieve the value of the string resource named 
              
          // "welcome", localized for the culture specified by ci.
              String str = rm.GetString("welcome"System.Globalization.CultureInfo.CurrentCulture);
              Console.WriteLine(str);
              }

          }

          資源文件必須寫成,程序集名字.資源文件名不包括擴(kuò)展名

          posted on 2009-03-30 19:49 何克勤 閱讀(4468) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 大埔县| 普兰县| 滁州市| 余江县| 嘉善县| 南乐县| 庆元县| 噶尔县| 北流市| 泰来县| 江达县| 礼泉县| 建瓯市| 庆云县| 安泽县| 翁源县| 南通市| 梅州市| 莲花县| 安化县| 洛南县| 依兰县| 砚山县| 金溪县| 普宁市| 河东区| 左权县| 探索| 高青县| 射阳县| 鄂尔多斯市| 上高县| 盐城市| 永仁县| 双峰县| 安阳市| 华阴市| 绥滨县| 哈密市| 木里| 商洛市|