SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Bechir Bejaoui Articles | Visual C# May 29, 2008
In this article, I will give one way to create a resource format file used to stock data about our resources through code.
Reader Level:
Download Files:
 

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!!!

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor