using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Resources; public class MainClass { public static void Main() { ResXResourceWriter w = new ResXResourceWriter(@"C:ResXForm.resx"); Image i = new Bitmap("happyDude.bmp"); w.AddResource("happyDude", i); w.AddResource("welcomeString", "Hello new resource format!"); w.Generate(); w.Close(); // Make a new *.resources file. ResourceWriter rw = new ResourceWriter(@"C:myResources.resources"); // Add 1 image and 1 string. rw.AddResource("happyDude", new Bitmap("happyDude.bmp")); rw.AddResource("welcomeString", "Hello new resource format!"); rw.Generate(); rw.Close(); } }
GUI Windows Form
Resource file generator
using System; using System.Resources; class LanguageResourceWriter { public static void WriteEnglishResources() { ResourceWriter rw = new ResourceWriter("Eng.resources"); rw.AddResource("Greeting", "Hello"); rw.AddResource("Program Name", "AAA"); rw.AddResource("Author", "T"); rw.Close(); } public static void WriteSpanishResources() { ResourceWriter rw = new ResourceWriter("Span.resources"); rw.AddResource("Greeting", "Hola"); rw.AddResource("Program Name", "BBB"); rw.AddResource("Author", "FFF"); rw.Close(); } public static void WriteFrenchResources() { ResourceWriter rw = new ResourceWriter("French.resources"); rw.AddResource("Greeting", "Bonjour"); rw.AddResource("Program Name", "DDD"); rw.AddResource("Author", "EEE"); rw.Close(); } public static void Main() { WriteEnglishResources(); WriteSpanishResources(); WriteFrenchResources(); } }
Writing a resource file programmatically.
using System; using System.Resources; class MainClass { public static void Main() { ResourceWriter rw = new ResourceWriter("English.resources"); rw.AddResource("PgmName", "AAA"); rw.AddResource("PgmVer", 1.0); rw.AddResource("PgmAuthor", "VVVV"); rw.Close(); } }
Make a new *.resources file.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Resources; public class MainClass { public static void Main() { ResourceWriter rw = new ResourceWriter(@"C:myResources.resources"); // Add 1 image and 1 string. rw.AddResource("happyDude", new Bitmap("happyDude.bmp")); rw.AddResource("welcomeString", "Hello new resource format!"); rw.Generate(); rw.Close(); } }
Check the key in a .resources file
using System; using System.Resources; using System.Collections; public class MainClass { public static void DisplayGreeting(string resName) { try { ResourceReader reader = new ResourceReader(resName + ".resources"); IDictionaryEnumerator dict = reader.GetEnumerator(); while (dict.MoveNext()) { string s = (string)dict.Key; if (s == "Greeting") Console.WriteLine("{0}", dict.Value); } } catch (Exception e) { Console.WriteLine("Exception creating manager {0}", e); return; } } public static void Main(string[] args) { DisplayGreeting(args[0]); } }
Reading resources
using System;
using System.Resources;
using System.Collections;
public class MainClass {
public static void DumpResources(string resName) {
ResourceReader reader = new ResourceReader(resName);
IDictionaryEnumerator en = reader.GetEnumerator();
while (en.MoveNext()) {
Console.WriteLine(“Resource Name: [{0}] = {1}”, en.Key, en.Value);
}
reader.Close();
}
public static void DumpAResource(string resName, string keyName) {
try {
ResourceManager rMgr = new ResourceManager(resName,
System.Reflection.Assembly.GetExecutingAssembly());
Console.WriteLine(“Resource: {0}”, rMgr.GetString(keyName));
} catch (Exception e) {
Console.WriteLine(“Exception creating manager {0}”, e);
return;
}
}
public static void Main(string[] args) {
for (int i = 0; i < args.Length; ++i)
DumpAResource("English", args[i]);
DumpResources("English1.resources");
}
}
[/csharp]
Reading from a String Resource
using System; using System.Drawing; using System.Windows.Forms; using System.Resources; using System.Reflection; public class MainForm { public static void Main() { ResourceManager FormResources = new ResourceManager("StringTable", Assembly.GetExecutingAssembly()); string Message; Message = FormResources.GetString("Message"); MessageBox.Show(Message); } }