Csharp/CSharp Tutorial/GUI Windows Forms/Resx

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create a Resx resource file

<source lang="csharp">using System; using System.Collections; using System.IO; using System.Resources; class MainClass {

   static void Main(string[] args)
   {
       string resXFile = "test.resx";
       string resKey = "myKey";
       string resValueFile = "myValue";
       using (ResXResourceWriter writer = new ResXResourceWriter(resXFile))
       {
           Console.WriteLine("Associating {0} with {1}"s contents", resKey, resValueFile);
           Console.Write("To {0}...", resXFile);
           using (ResXResourceReader reader = new ResXResourceReader(resXFile))
           {
               foreach (DictionaryEntry node in reader)
                   writer.AddResource((string)node.Key, node.Value);
           }
           writer.AddResource(resKey, File.ReadAllBytes(resValueFile));
       }
   }

}</source>

Make a resx writer and specify the file to write to

<source lang="csharp">using System; using System.Resources; using System.Drawing; using System.Collections; using System.Windows.Forms; public class MainClass{

 public static void Main(){
   
   ResXResourceWriter w = new ResXResourceWriter("ResXForm.resx");
   Image i = new Bitmap("YourFile.bmp");
   w.AddResource("happyDude", i);
   
   w.AddResource("welcomeString", "Hello new resource format!");
   w.Generate();
   w.Close();
 }

}</source>

ResXResourceWriter and ResXResourceReader

<source lang="csharp">using System; using System.Resources; using System.Drawing; using System.Collections; using System.Windows.Forms; public class MainClass{

 public static void Main(){
   
   ResXResourceWriter w = new ResXResourceWriter("ResXForm.resx");
     
   Image i = new Bitmap("YourFile.bmp");
   w.AddResource("happyDude", i);
   
   w.AddResource("welcomeString", "Hello new resource format!");
     
   w.Generate();
   w.Close();
     
   ResXResourceReader r = new ResXResourceReader("ResXForm.resx");
   
   IDictionaryEnumerator en = r.GetEnumerator();
   while (en.MoveNext()) 
   {
     Console.WriteLine("Value:" + en.Value.ToString() + " Key: " + en.Key.ToString());
   }
   r.Close();
  }

}</source>

Value:System.Drawing.Bitmap Key: happyDude
Value:Hello new resource format! Key: welcomeString