Get country list from windows registry
When You want to create a List object populated with country name, you didn’t have to create a separate list of file or database that contain that country list. Simply just use the the entry from windows registry to have it. I’ll show You how. Here is the code snippets :
CountryList.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
namespace net.de.common.l10n
{
///
/// This class provide method to retrieve country list from windows registry
/// and put it in a list object
///
class CountryList
{
// Initialize static CountryList object
private static CountryList countryList = null;
///
/// Default constructor with no parameter
///
public CountryList() { }
///
/// Get an instance of CountryList object. Only one instance of CountryList
/// object is available
///
/// CountryList
public static CountryList getInstance(){
// If the instance is null, mean it's not exist
// then create a new one
if(countryList == null){
return new CountryList();
}
// Return the instance that already exist
return countryList;
}
///
/// Method to retrieve the list of country from the windows registry
///
/// List
public List getCountries() {
List returnValue = new List();
// Get the registry value
RegistryKey countries = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion" +
"\\Telephony\\Country List");
if (countries != null) {
string[] subKeys = countries.GetSubKeyNames();
// For each country code in the registry
foreach(string ccode in subKeys){
RegistryKey country = countries.OpenSubKey(ccode);
returnValue.Add(country.GetValue("Name").ToString());
// Closing the registry key object
country.Close();
}
// Closing the registry object
countries.Close();
}
// Used to sort some items in the List object
returnValue.Sort();
// Return the List object
return returnValue;
}
}
}
In this clas I used the factory pattern to create a common utility that will only produce a single instance of this Object. It’s simply to understand, just make sure You add the Microsoft.Win32 in the using declaration. After You create this, maybe You want to create a custom country ComboBox control and place it in the toolbox. I’ll explain how to do this in my next blog post.
Leave a Reply