Skip to content

How to create a Resx Resource file using C#

May 5, 2011

I had found something which may or may not be useful, and this is creating resource files that is used extensively for Visual Studio that can be used for storing strings, textboxes, checkboxes, etc for ASP.NET components.

You can use the System.Resources.ResourceWriter class to programmatically create a binary resource (.resources) file directly from code which can potentially hold object data like strings , binary data like byte arrays or your own customised objects stored in Microsoft’s form of XML. This is useful for localisation for other languages using Google APIs which I can explain further in a later post.

The code essentially: –

  • Creates a custom class called Location (I used this example as it ties with my WP7 application I am writing) that holds string data and other things like doubles, and includes a constructor method.
  • Creates a ResourceWriter object with a unique file name (here its called Location.resources), note: you have to add .resources to the extension of the file. You can do this via specifying a file name or a file stream to a ResourceWriter class constructor.
  • Call one of the overloads of the ResourceWriter.AddResource method for each named resource object that is added to the resource file, buit like Document XML. The resource can be a string, an object, or a collection of binary data (a byte array) which in my case I have added my custom class Location.
  • Finally call the ResourceWriter.Close method to write the resources to the file.

Here is the code: –

[Serializable()]

public class Location

{

    private string locationName;

private string locationDescription;

    private double locationLong;

    private double locationLat;

    public Location(string lname, string ldesc) :

        this(lname, ldesc, 0, 0){}

    public Location(string lname, string ldesc, double llong, double llat)

    {

        this.locationName = lname;

        this.locationDescription = ldesc;

        this.locationLong = llong;

        this.locationLat = llat;

    }

    public string Name

    {

        get { return this.locationName; }

    }

    public string Description

    {

        get { return this.locationDescription; }

    }

    public double Long

    {

        get { return this.locationLong; }

    }

    public double Lat

    {

        get { return this.locationLat; }

    }

    protected void CreateResxFile()

    {

        // Instantiate an Location object.

        Location l1 = new Location(“Westminster”, “Inner borough of central Greater London, located on the north bank of the River Thames between Kensington and the City of London.”, -0.1667, 51.5000);

        Location l2 = new Location(“Manchester”, “Metropolitan district in the metropolitan county of Greater Manchester; it contains the city of Manchester.”, -2.2500, 53.4833);

        // Define a resource file named Location.resx

        using (ResourceWriter rw = new ResourceWriter(@”.Location.resources”))

{

rw.AddResource(“Title”, “Locations”);

rw.AddResource(“HeaderString1”, “Location Name”);

           rw.AddResource(“HeaderString2”, “Location Latitude”);

           rw.AddResource(“HeaderString3”, “Location Longititude”);

           rw.AddResource(“HeaderString4”, “Location Description”);

           rw.AddResource(“DefaultLocation1”, l1);

       }

    }

Hope this helps!

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: