---
title: "Get countries from system"
author: "wangdeshui"
date: "2009-05-08T16:02:00Z"
tags: []
slug: "get-countries-from-system"
draft: false
lastmod: "2009-05-08T16:02:00Z"
id: 404922
url: "https://wangdeshui.iteye.com/blog/404922"
---

```
   1: using System;
```

```
   2: using System.Collections.Generic;
```

```
   3: using System.Linq;
```

```
   4: using System.Text;
```

```
   5: using System.Globalization;
```

```
   6: using System.IO;
```

```
   7:  
```

```
   8: namespace CultureInformation
```

```
   9: {
```

```
  10:     class Program
```

```
  11:     {
```

```
  12:         static void Main(string[] args)
```

```
  13:         {
```

```
  14:             StreamWriter sw = new StreamWriter(@"D:\PopulateTableData.Txt");
```

```
  15:             CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
```

```
  16:            
```

```
  17:             StringBuilder sb = new StringBuilder();
```

```
  18:  
```

```
  19:             sb.AppendLine(" declare @LocalizableId bigint, @CultureId bigint,@CurrencyId bigint ");
```

```
  20:  
```

```
  21:             //insert Culture
```

```
  22:             foreach (CultureInfo item in cultures)
```

```
  23:             {
```

```
  24:                 sb.AppendLine(string.Format("INSERT INTO [LocalizableText] ([CultureInvariantText]) VALUES('{0}') ", item.EnglishName.Replace("'", "''")));
```

```
  25:                 sb.AppendLine(" set @LocalizableId=IDENT_CURRENT('LocalizableText') ");
```

```
  26:                 sb.AppendLine(string.Format("INSERT INTO [Culture]([Code],[Name]) VALUES('{0}',@LocalizableId)", item.Name));
```

```
  27:             }
```

```
  28:  
```

```
  29:             //insert currency
```

```
  30:             List<string> currencyList = new List<string>();
```

```
  31:             foreach (CultureInfo item in cultures)
```

```
  32:             {
```

```
  33:                 RegionInfo r = new RegionInfo(item.LCID);
```

```
  34:  
```

```
  35:                 if (!currencyList.Contains(r.ISOCurrencySymbol))
```

```
  36:                 {
```

```
  37:                     currencyList.Add(r.ISOCurrencySymbol);
```

```
  38:                     sb.AppendLine(string.Format("INSERT INTO [LocalizableText] ([CultureInvariantText]) VALUES ('{0}')", r.CurrencyEnglishName.Replace("'", "''")));
```

```
  39:                     sb.AppendLine(" set @LocalizableId=IDENT_CURRENT('LocalizableText') ");
```

```
  40:                     sb.AppendLine(string.Format("INSERT INTO Currency ([IsoCode],[Name]) VALUES ('{0}',@LocalizableId)", r.ISOCurrencySymbol));
```

```
  41:                 }
```

```
  42:             }
```

```
  43:  
```

```
  44:             // insert country
```

```
  45:             List<string> cultureList = new List<string>();
```

```
  46:             foreach (CultureInfo item in cultures)
```

```
  47:             {
```

```
  48:                 RegionInfo r = new RegionInfo(item.LCID);
```

```
  49:                 if (!(cultureList.Contains(r.ThreeLetterISORegionName)))
```

```
  50:                 {
```

```
  51:                     cultureList.Add(r.ThreeLetterISORegionName);
```

```
  52:                     if (r.TwoLetterISORegionName.Length > 2) continue;
```

```
  53:                     sb.AppendLine(string.Format("INSERT INTO [LocalizableText] ([CultureInvariantText]) VALUES ('{0}')", r.EnglishName.Replace("'", "''")));
```

```
  54:                     sb.AppendLine(" set @LocalizableId=IDENT_CURRENT('LocalizableText') ");
```

```
  55:                     sb.AppendLine(string.Format(" select @CultureId=[CultureId] from [Culture] where [Code]='{0}'", item.Name));
```

```
  56:                     sb.AppendLine(string.Format("  select @CurrencyId=[CurrencyId] from [Currency] where [IsoCode]='{0}'", r.ISOCurrencySymbol));
```

```
  57:                     sb.AppendLine(string.Format("INSERT INTO Country([IsoCode3],[IsoCode2],[CurrencyId],[CultureId],[Name]) VALUES('{0}','{1}',@CurrencyId,@CultureId,@LocalizableId)",
```

```
  58:                                 r.ThreeLetterISORegionName, r.TwoLetterISORegionName));
```

```
  59:                 }
```

```
  60:  
```

```
  61:             }
```

```
  62:  
```

```
  63:  
```

```
  64:             sw.WriteLine(sb.ToString());
```

```
  65:  
```

```
  66:             sw.Flush();
```

```
  67:             sw.Close();
```

```
  68:             Console.ReadLine();
```

```
  69:         }
```

```
  70:  
```

```
  71:  
```

```
  72:         public static List<string> GetCountryList()
```

```
  73:         {
```

```
  74:  
```

```
  75:             //create a new Generic list to hold the country names returned
```

```
  76:             List<string> cultureList = new List<string>();
```

```
  77:             //create an array of CultureInfo to hold all the cultures found, these include the users local cluture, and all the
```

```
  78:  
```

```
  79:             //cultures installed with the .Net Framework
```

```
  80:             CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
```

```
  81:  
```

```
  82:             //loop through all the cultures found
```

```
  83:             foreach (CultureInfo culture in cultures)
```

```
  84:             {
```

```
  85:                 //pass the current culture's Locale ID (http://msdn.microsoft.com/en-us/library/0h88fahh.aspx)
```

```
  86:                 //to the RegionInfo contructor to gain access to the information for that culture
```

```
  87:                 RegionInfo region = new RegionInfo(culture.LCID);
```

```
  88:                 //make sure out generic list doesnt already
```

```
  89:                 //contain this country
```

```
  90:                 if (!(cultureList.Contains(region.EnglishName)))
```

```
  91:  
```

```
  92:                     //not there so add the EnglishName (http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo.englishname.aspx)
```

```
  93:  
```

```
  94:                     //value to our generic list
```

```
  95:                     cultureList.Add(region.EnglishName);
```

```
  96:             }
```

```
  97:             return cultureList;
```

```
  98:  
```

```
  99:         }
```

```
 100:     }
```

```
 101: }
```