code_junkie

Compact Framework - Retrieve a list of countries and regions

Nov 13th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace OpenNETCF.Globalization
  7. {
  8. public class CultureInfoHelper
  9. {
  10. private delegate int EnumLocalesHandler(string lpLocaleString);
  11.  
  12. private static EnumLocalesHandler m_localesDelegate;
  13.  
  14. private static List<CultureInfo> m_cultures;
  15.  
  16. private static int EnumLocalesProc(string locale)
  17. {
  18. try
  19. {
  20. m_cultures.Add(CultureInfo.GetCultureInfo(
  21. int.Parse(locale, NumberStyles.HexNumber)));
  22. }
  23. catch
  24. {
  25. // failed for this locale - ignore and continue
  26. }
  27.  
  28. return 1;
  29. }
  30.  
  31. public static CultureInfo[] GetCultures()
  32. {
  33. if (m_localesDelegate == null)
  34. {
  35. m_cultures = new List<CultureInfo>();
  36. m_localesDelegate = new EnumLocalesHandler(EnumLocalesProc);
  37. IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate(
  38. m_localesDelegate);
  39. int success = EnumSystemLocales(fnPtr, LCID_INSTALLED);
  40. }
  41.  
  42. return m_cultures.ToArray();
  43. }
  44.  
  45. private const int LCID_INSTALLED = 0x01;
  46. private const int LCID_SUPPORTED = 0x02;
  47.  
  48. [DllImport("coredll", SetLastError = true)]
  49. private static extern int EnumSystemLocales(
  50. IntPtr lpLocaleEnumProc, uint dwFlags);
  51. }
  52. }
  53.  
  54. using OpenNETCF.Globalization;
  55. ....
  56. static void Main()
  57. {
  58. foreach (CultureInfo ci in CultureInfoHelper.GetCultures())
  59. {
  60. Debug.WriteLine(string.Format("0x{0:x2}({1}) : {2}", ci.LCID, ci.Name, ci.EnglishName));
  61. }
  62. }
  63.  
  64. 0x402(bg-BG) : Bulgarian (Bulgaria)
  65. 0x403(ca-ES) : Catalan (Catalan)
  66. 0x405(cs-CZ) : Czech (Czech Republic)
  67. 0x406(da-DK) : Danish (Denmark)
  68. 0x407(de-DE) : German (Germany)
  69. 0x408(el-GR) : Greek (Greece)
  70. 0x409(en-US) : English (United States)
  71. ...
  72. 0x400a(es-BO) : Spanish (Bolivia)
  73. 0x440a(es-SV) : Spanish (El Salvador)
  74. 0x480a(es-HN) : Spanish (Honduras)
  75. 0x4c0a(es-NI) : Spanish (Nicaragua)
  76. 0x500a(es-PR) : Spanish (Puerto Rico)
Add Comment
Please, Sign In to add comment