Extracting data from a resx file using c#

.Net has a built-in reader (ResxResourceReader) and writer (ResxResourceWriter) which makes it really easy to read a resx file. The classes can be found in the System.Resources namespace under the System.Windows.Forms assembly (in System.Windows.Forms.dll)  

Basic Reader Example:

            var rsxr = new ResXResourceReader("demo.resx");

            foreach (DictionaryEntry d in rsxr)
                Console.WriteLine(pathRelativeToStartingPath + ",\"" + d.Key + "\",\"" + d.Value + "\"");

Basic Writer Example:

            string txt = "One smart fellow, he felt smart";
            string txt_fr = "Un garçon intelligent, il se sentait à puce";

            ResXResourceWriter rsxw = new ResXResourceWriter("demo.resx");
            rsxw.AddResource("MyText", txt);

            ResXResourceWriter rsxw_fr = new ResXResourceWriter("demo.fr-FR.resx");
            rsxw_fr.AddResource("MyText", txt_fr);
Of course if you are putting images or media into your resx file you'll additional code to handle the different types that value can be.

No comments:

Post a Comment