Following are the step by step process to create, read and write into resource file using code.
1.Open a new project in c# .
2. Add a ResourceFile.resx in the project.
3. Add a new class and name it as ResourceCreator
4. Add these following namespaces:
using System.Windows.Forms;
using System.Resources;
using System.Drawing;
using System.IO;
5.Add the folling code snippet in ResourseCreator class
public static void Create(string resFileName, string oString, string imagePath)
{
try
{
Image oImage = Image.FromFile(imagePath);
ResXResourceWriter resourceWriter = newResXResourceWriter(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);
}
}
6.Call this method by ResourceCreater.Create(@”D:\Resource.resx”,”first”,@”D:\Image1.jpg””);
7.Add a new class and name it as ResourceRetriever
8.Add those namespaces:
using System.Windows.Forms;
using System.Resources;
using System.Collections;
9.Add this code in ResourceRetriever class
public static void Retrieve(string resPathName)
{
try
{
//Create a new ResXResource reader and set the resx path to resxPathName
ResXResourceReader resReader = newResXResourceReader(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);
}
}
10.Call this method by ResourceRetriver.Retrieve(@”D:\Resource.resx”);