WPF中 遍历Properties下的资源文件
直接代码:
/// <summary> /// 根据省份获得该省所有的城市名 /// </summary> /// <param name="Province"></param> /// <returns></returns> public Dictionary<string,string> GetCityByPro(string proKey) { Dictionary<string, string> city = new Dictionary<string, string>(); //检索指定的资源文件(资源文件名为当前名称空间.Properties.BaseName) ResourceManager rm = new ResourceManager("PC_Client.Properties.DataBase", Assembly.GetExecutingAssembly()); //根据指定的资源文件获取resourceset对象 ResourceSet set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true); //根据获得的省份的Key值来获取所有的城市名称 foreach (DictionaryEntry item in set) { string strKey = item.Key.ToString(); string[] keys = strKey.Split('_'); string str = keys[0] + "_" + keys[1]; if ((keys.Count() > 2) && (str == proKey)) { city.Add(item.Key.ToString(), item.Value.ToString()); } } return city; } /// <summary> /// 获得所有的省份列表 /// </summary> /// <returns></returns> public Dictionary<string,string> GetAllProvinces() { Dictionary<string, string> provinces = new Dictionary<string, string>(); //对指定的资源文件进行检索 ResourceManager rm = new ResourceManager("PC_Client.Properties.DataBase", Assembly.GetExecutingAssembly()); //获取指定资源文件的resourceSet对象 ResourceSet set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true); //遍历指定资源文件的键值对 foreach (DictionaryEntry each in set) { string str = each.Key.ToString(); string[] strs = str.Split('_'); if (strs.Count() <= 2) { provinces.Add(each.Key.ToString(),each.Value.ToString()); } } return provinces; } /// <summary> /// 获得所有的城市列表 /// </summary> /// <returns></returns> public Dictionary<string, string> GetAllCities() { Dictionary<string, string> cities = new Dictionary<string, string>(); //检索指定的资源文件 ResourceManager rm = new ResourceManager("PC_Client.Properties.DataBase", Assembly.GetExecutingAssembly()); //根据指定的资源文件获取resourceSet对象 ResourceSet set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true); //对当前的resourceset对象的键值对遍历 foreach (DictionaryEntry item in set) { string str = item.Key.ToString(); string[] strs = str.Split('_'); if (strs.Count() > 2) { cities.Add(item.Key.ToString(),item.Value.ToString()); } } return cities; }