Wednesday, November 9, 2011

Read and Write into a resx file programmatically

In this article, i will show you how to create a Resources file(.resx) into you'r project through programming.

Step 1
Create a Console application and give name it ConResource

Step 2
First we have to create a Resources file then we add key with value,In Program class a create static method for creating resources file and value.it is look like this


public static void Create()
{
try
{
// Create a instance of ResourceWriter and specify the name of the resource file.
System.Resources.ResourceWriter RWObj = new ResourceWriter(AppDomain.CurrentDomain.BaseDirectory + "MyResource.resx");

// Add String resource
RWObj.AddResource("FirstName", "Kishor");
RWObj.AddResource("LastName", "Naik");

// Close the Resource Writer
RWObj.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

Step 3
Read the Resources file,it is look like this

public static void Read()
{
try
{
// Create a instance of the ResourceReader and specify location of the resource file
System.Resources.ResourceReader RRobj = new ResourceReader(AppDomain.CurrentDomain.BaseDirectory + "MyResource.resx");

// Read a resource file
foreach (System.Collections.DictionaryEntry DE in RRobj)
{
try
{
System.Console.WriteLine(DE.Key + "\t:\t" + DE.Value);
}
catch (Exception)
{ }
}

// Close the ResourceReader
RRobj.Close();

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

Step 4
Call above methods in Main function,it is look like this

static void Main(string[] args)
{
try
{

Create(); // Create a Resource file and add a value
Read(); // read a resource file

}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}

Full Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Collections;

namespace ConResource
{
public class Program
{
static void Main(string[] args)
{
try
{

Create(); // Create a Resource file and add a value
Read(); // read a resource file

}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}

#region Methods

public static void Create()
{
try
{
// Create a instance of ResourceWriter and specify the name of the resource file.
System.Resources.ResourceWriter RWObj = new ResourceWriter(AppDomain.CurrentDomain.BaseDirectory + "MyResource.resx");

// Add String resource
RWObj.AddResource("FirstName", "Kishor");
RWObj.AddResource("LastName", "Naik");

// Close the Resource Writer
RWObj.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

public static void Read()
{
try
{
// Create a instance of the ResourceReader and specify location of the resource file
System.Resources.ResourceReader RRobj = new ResourceReader(AppDomain.CurrentDomain.BaseDirectory + "MyResource.resx");

// Read a resource file
foreach (System.Collections.DictionaryEntry DE in RRobj)
{
try
{
System.Console.WriteLine(DE.Key + "\t:\t" + DE.Value);
}
catch (Exception)
{ }
}

// Close the ResourceReader
RRobj.Close();

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

#endregion
}
}

Download
Download Source Code