posts - 134,comments - 22,trackbacks - 0

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

          檢索資源

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

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

          }

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

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

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


          網站導航:
           
          主站蜘蛛池模板: 芜湖市| 庆阳市| 抚州市| 尼木县| 沁阳市| 尼勒克县| 泉州市| 浪卡子县| 芮城县| 永春县| 犍为县| 呼伦贝尔市| 綦江县| 临洮县| 留坝县| 渭源县| 成都市| 广宁县| 永丰县| 句容市| 黄大仙区| 商南县| 夏河县| 治县。| 塘沽区| 惠水县| 亳州市| 娱乐| 安义县| 海淀区| 西华县| 积石山| 枣阳市| 玛多县| 和平区| 芜湖县| 诏安县| 内乡县| 涡阳县| 隆安县| 罗田县|