Handle resource files - read and write into a resource file programmatically: Part IV

In this article, I will give one way to create a resource format file used to stock data about our resources through code. Remember that resources files format are binary format used to stock data about none executables elements, it is possible, to embed those kinds of files within the executable one when creating the .msi setup. 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 resources files and the other files formats used in the same context.

The purpose here is to generate a resource file as a first step, then, read its contents programmatically 

Walkthrough 1:

Open a new project, then add a new class and name it ResourceCreator

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 resource file. Same objects used in the previous article Part III handle resource files except that in this case we will create a resource file instead:

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:

Note that we will use the ResourceWriter instead of the ResXResourceWriter class for some one who has read the Part I handle resource files.

Add a public static method into the ResourceCreator class

/// <summary>
///
/// </summary>
/// <param name="resxFileName">Indicates the complete resource 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 resFileName, string oString, string imagePath)
{
    try
    {
        Image oImage = Image.FromFile(imagePath);
        ResourceWriter resourceWriter = new ResourceWriter(resFileName);
        resourceWriter.AddResource("myImage", oImage);
        resourceWriter.AddResource("myString", oString);
        resourceWriter.Close();
        MessageBox.Show("File " + resFileName + " created");
    }
    //if the file path is wrong or dosn't found
    catch (FileNotFoundException caught)
    {
        MessageBox.Show("Source: " + caught.Source + " Message: " + caught.Message);
    }
}

You can call the method above using this code:

ResourceCreator.Create(@"C:\myResourceFile.resource","BejaouiBechir", @"C:\bechir.bmp");

Now, browse to the C:\, you will find there, a file that looks like ; this is the resource file icon. That means the resource file is created successfully. If you want retrieve data, it is possible to achieve such goal using a ResourceReader object.

Walkthrough 2:

Create a new class and give it ResourceRetriever as a name

First of all, add

using System.Collections;

into the namespaces list

Then add a public static void method as follow:

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

Then call the method using this code.

ResourceRetriever.Retrieve(@"C:\myResourceFile.resource");

Good dotneting!!!


Similar Articles