Display local information using RegionInfo and CultureInfo Class

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 5999 red flag

How to display country information using RegionInfo class

Display local information using RegionInfo and CultureInfo Class

In this article we will discuss one very important class of Globalization namespace called RegionInfo class. In modern web application globalization is very important because web page is not restricted to certain country or region.

So, if we want to make necessary change in our web application then we have to get information of visitor’s location. And according to location we can customize our application. Thus we can achieve Globalization in web application.

Now, how we will get local information of visitor? The solution is RegionInfo class. Have a look on below code.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
 
            RegionInfo Country = new RegionInfo("IN");
            Console.WriteLine("Country infroation using RegionInfo Class");
            Console.WriteLine("*******************************************************");
            Console.WriteLine("Country Name:           {0}", Country.Name);
            Console.WriteLine("Display Name:           {0}", Country.DisplayName);
            Console.WriteLine("English Name:           {0}", Country.EnglishName);
            Console.WriteLine("IsMetric:               {0}", Country.IsMetric);
            Console.WriteLine("Three Letter ISO Region Name:      {0}", Country.ThreeLetterISORegionName);
            Console.WriteLine("Three Letter Windows Region Name:  {0}", Country.ThreeLetterWindowsRegionName);
            Console.WriteLine("Two Letter ISO Region Name:        {0}", Country.TwoLetterISORegionName);
            Console.WriteLine("Currency Symbol:       {0}", Country.CurrencySymbol);
            Console.WriteLine("ISO Currency mbol:              {0}", Country.ISOCurrencySymbol);
           
            Console.ReadLine();
        }
    }
}


Within Main() function we have defined RegionInfo class using below line.

 RegionInfo Country = new RegionInfo("IN");

Here constructor of RegionInfo class taking one argument. The argument is nothing but two character country code. Here IN for India, If you want to get local information of other country just use two character country code like US for USA etc.

Then we can use various property of Country object of RegionInfo class to retrieve information.

Like if we use Name property then it will display country name and so on.

Display Date according to region

Just now we have learned how to display country information using RegionInfo class but one most important concept we have not covered yet.

Date time formatting is very essential concept of globalization. According to country and region we have to formatted date and time information. Here we will see how to get date format automatically using CultureInfo class.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            DateTime d = DateTime.Now;
            Console.WriteLine("Date format of France : "+ d.ToShortDateString());
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-IN");
            d = DateTime.Now;
            Console.WriteLine("Date format of India : " + d.ToShortDateString());
            Console.ReadLine();
        }
    }


Here is output.

At first we have set current culture is France country then we have changed country France to India and based on the change the format also changed automatically.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)