如何使用system .globalization namspace获取区域列表 [英] How to get the list of regions using system .globalization namspace

查看:55
本文介绍了如何使用system .globalization namspace获取区域列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否知道如何使用 System.globalization 命名空间获取区域列表。

解决方案

< pre lang =c ++> // 代码段
/// < 摘要 >
/// 生成国家/地区列表的方法,比如填充
/// 一个ComboBox,与国家选择。我们在通用列表 /// 值summarycomment>< t >
/// < / t > < /摘要 >
/// < 返回 > < / returns >
public static List< string> GetCountryList()
{
// 创建一个新的通用列表来保存返回的国家/地区名称
List< string> cultureList = new List< string>();

// 创建一个CultureInfo数组来保存找到的所有文化,包括用户本地文化,以及与.Net Framework一起安装的所有
// 文化
CultureInfo [] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures& ~HeultureTypes.NeutralCultures);

// 遍历所有找到的文化
foreach( CultureInfo culture in cultures)
{
// 传递当前文化的区域设置ID(http://msdn.microsoft.com/en-us/library/0h88fahh.aspx)
/ / 到RegionInfo构造函数以获取对该文化的信息的访问权
RegionInfo region = new RegionInfo(culture.LCID);

// 确保通用列表尚未
< span class =code-comment> // 包含此国家/地区
if (!(cultureList.Contains(region.EnglishName)))
// 不存在所以添加英文名称(http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo.englishname.aspx)
// 我们的通用列表的值
cultureList.Add(region.EnglishName);
}
return cultureList;
}

// 使用示例
foreach (字符串country in GetCountryList())
{
comboBox1.Items.Add(country);
}< / string>< / string>< / string>


  public   class 国家/地区
{
public string countryCode { get ; set ; }
public string countryName { get ; set ; }
}

public 列表<国家> GetCountries()
{
// 创建一个新的通用列表来保存返回的国家/地区名称
列表<国家> countryList = new List< Country>();

CultureInfo [] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures& ~HeultureTypes.SpecificCultures);

var v = cultures.Where(C = > C.LCID != 127 &!C.IsNeutralCulture)。选择(C = > new { new RegionInfo(C.LCID).EnglishName, new RegionInfo(C.LCID).Name})。ToList();

countryList = v.GroupBy(C = > new {C EnglishName,C.Name})。选择(C = > new Country(){countryCode = C .Key.Name,countryName = C.Key.EnglishName})。OrderBy(C = > C.countryName)。ToList();

return countryList;
}


May I know how to get the list of regions using System.globalization namespace.

解决方案

//Code snippet
/// <summary>
/// method for generating a country list, say for populating
/// a ComboBox, with country options. We return the
/// values in a Generic List<t>
/// </t></summary>
/// <returns></returns>
public static List<string> GetCountryList()
{
    //create a new Generic list to hold the country names returned
    List<string> cultureList = new List<string>();

    //create an array of CultureInfo to hold all the cultures found, these include the users local cluture, and all the
    //cultures installed with the .Net Framework
    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

    //loop through all the cultures found
    foreach (CultureInfo culture in cultures)
    {
        //pass the current culture's Locale ID (http://msdn.microsoft.com/en-us/library/0h88fahh.aspx)
        //to the RegionInfo contructor to gain access to the information for that culture
        RegionInfo region = new RegionInfo(culture.LCID);

        //make sure out generic list doesnt already
        //contain this country
        if (!(cultureList.Contains(region.EnglishName)))
            //not there so add the EnglishName (http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo.englishname.aspx)
            //value to our generic list
            cultureList.Add(region.EnglishName);
    }
    return cultureList;
}

//Example usage
foreach(string country in GetCountryList())
{
    comboBox1.Items.Add(country);
}</string></string></string>


public class Country
    {
        public string countryCode { get; set; }
        public string countryName { get; set; }
    }

public List<Country> GetCountries()
        {
            //create a new Generic list to hold the country names returned
            List<Country> countryList = new List<Country>();

            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.SpecificCultures);

            var v = cultures.Where(C => C.LCID != 127 & !C.IsNeutralCulture).Select(C => new { new RegionInfo(C.LCID).EnglishName, new RegionInfo(C.LCID).Name }).ToList();

            countryList = v.GroupBy(C => new { C.EnglishName, C.Name }).Select(C => new Country() { countryCode = C.Key.Name, countryName = C.Key.EnglishName }).OrderBy(C => C.countryName).ToList();

            return countryList;
        }


这篇关于如何使用system .globalization namspace获取区域列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆