Handle resource files - Read and Write into a resx file programmatically: Part III

In this article, I will give a trick of how to create a resx file to stock data about our resources using code. Remember that resx files format are kind of XML files used only to stock data about none executables elements. For someone how doesn't read the Part I handle resource files, I suggest that he takes a look on it before reading this article in order to have an idea about resx files and the other resources files formats in general.

The purpose here is to avoid create such file manually using a text editor or something like that, especially when the background knowledge concerning XML and resx files structure is insufficient.

Walkthrough1:

Open a new project and add a new class into it, then name it ResxCreator

First of all, add those namespaces:

using System.Windows.Forms;
using System.Resources;
using System.Drawing;
using System.IO;

In this class we will add an Image and a string into a resx file:

Type  Name
String  Bechir Bejaoui
Image C:\Bechir.bmp

Note: Don't use format other than bitmaps (*.bmp) or device independent bitmaps (*.dib). Otherwise, a FileNotFoundException will be thrown.

Now, add a static void method to your class and implement it as bellow:

/// <summary>
///
/// </summary>
/// <param name="resxFileName">Indicates the complete resx file name including its path</param>
/// <param name="oString">The string parmaeter introduced by the developer or the user </param>
/// <param name="imagePath">Indicates the complete image file including its path</param>
public static void Create(string resxFileName, string oString,string imagePath)
{
    ResXResourceWriter resxWriter;
    try
    {
        //Define and assign and image object
        Image oImage;
        oImage = Image.FromFile(imagePath);
        /*Instanciate a ResXResourceWriter object in orderto create
         * or/and write into a given resx file */
        resxWriter = new ResXResourceWriter(resxFileName);
        //Add an Image resource
        resxWriter.AddResource("myImage", oImage);
        //Add a string resource
        resxWriter.AddResource("myString", oString);
        MessageBox.Show("File" + resxFileName + " created");
        //Close the resxWriter
        resxWriter.Close();
    }
    //if the file path is wrong or dosn't found
    catch (FileNotFoundException caught)
    {
        MessageBox.Show("Source: " + caught.Source + " Message: " + caught.Message);
    }
}
 
//Then, call it form your code:

ResxCreator.Create(@"C:\myResxFile.resx", "Bechir Bejaoui", @"C:\bechir.bmp");

The ResXResourceWriter is the class used to write and/or create a resx file in a given location.

Now, browse to the C:\, you will find there, a file that looks like ; this is the resx file icon. That means the resx file is created successfully.

If you want retrieve data from a resx file, it is possible using a ResXResourceReader object to achieve such goal.

Walkthrough 2:

Create a new class and call it ResxRetriever. The mission of this class is to retrieve data from a given resx file.  Let's do that using the previous resx file, the one created in the walkthrough1. Remember that it contains a string and an image.

First of all, add

using System.Collections; 

into the namespaces list

Add a public static void method and implement it as below:

/// <summary>
///
/// </summary>
/// <param name="resxPathName">Indicates the resx file location</param>
public static void Retrieve(string resxPathName)
{
    ResXResourceReader resxReader;
    try
    {
        //Create a new ResXResource reader and set the resx path to resxPathName
        resxReader = new ResXResourceReader(resxPathName);
        //Enumerate the elements within the resx file and dispaly them
        foreach (DictionaryEntry d in resxReader)
        {
            MessageBox.Show(d.Key.ToString() + " : " + d.Value.ToString());
        }
        //Close the resxReader
        resxReader.Close();
        MessageBox.Show("Done");
    }
    //If the resx file represents some incoherences 
    catch (ArgumentException caught)
    { MessageBox.Show("Source: " + caught.Source + "Message: " + caught.Message); } 
}

Then call it from your code:

ResxRetriever.Retrieve(@"C:\myResxFile.resx");

The resx file represents some incoherence an Argument Exception will be thrown. By catching this last one, you can locate exactly where and what kind of problem is, then browse to resx file, open it with a text editor then try to maintain it so that it could be readable after.

Good dotneting!!!


Recommended Free Ebook
Similar Articles