internal static IOrderedEnumerable <KeyValuePair <DateTime, string> > GetBackupFiles(string output, string db) { var retVal = new System.Collections.Generic.Dictionary <DateTime, string>(); var allBackups = new List <string>(); allBackups.AddRange(Directory.GetFiles(output, "*.zip")); allBackups.AddRange(Directory.GetFiles(output, "*.bak")); foreach (var bFile in allBackups) { if (Regex.IsMatch(Path.GetFileNameWithoutExtension(bFile), string.Format(@"{0}_._\d+", db))) { var fDate = Path.GetFileNameWithoutExtension(bFile).Split('_')[Path.GetFileNameWithoutExtension(bFile).Split('_').Length - 1]; var properStr = fDate.Insert(4, "-").Insert(7, "-").Insert(10, " ").Insert(13, ":"); var date = DateTime.MinValue; if (DateTime.TryParse(properStr, out date) && !retVal.ContainsKey(date)) { retVal.Add(date, bFile); } } } return(retVal.OrderByDescending(x => x.Key)); }
/// <summary> /// 将集合内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA /// </summary> /// <param name="dictionarys">参数(键值对)字典</param> /// <param name="isAsc">参数(键值对)字典的主键排序</param> /// <returns></returns> public static System.Collections.Generic.Dictionary <string, string> Sort(this System.Collections.Generic.Dictionary <string, string> dictionarys, bool isAsc = true) { System.Collections.Generic.Dictionary <string, string> newDictionarys = new System.Collections.Generic.Dictionary <string, string>(); foreach (System.Collections.Generic.KeyValuePair <string, string> item in dictionarys) { if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value)) { newDictionarys.Add(item.Key, item.Value); } } if (isAsc) { return(newDictionarys.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value)); //将集合内非空参数值的参数按照参数名ASCII码从小到大排序(字典序) } else { return(newDictionarys.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value)); //将集合内非空参数值的参数按照参数名ASCII码从大到小排序(字典降序) } }