/// <summary>保存到配置文件中去</summary> /// <param name="filename">文件名</param> public override void Save(string filename) { if (filename.IsNullOrWhiteSpace()) { filename = DirEx.CurrentDir() + ConfigFile; } if (filename.IsNullOrWhiteSpace()) { throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!"); } ConcurrentDictionary <string, Ident> idents = ConfigHelper.InitIdents(current); ConfigHelper.SaveConfigValue(Current, idents); List <string> strs = new List <string> { "<?xml version=" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "utf-8" + "\"" + "?> ", "<!--" + Description + "--> " }; strs.Add("<" + GetType().Name + ">"); SortedList <int, Ident> slist = new SortedList <int, Ident>(); foreach (var ident in idents.Values) { slist.Add(ident.Index, ident); } foreach (var ident in slist.Values) { if (!ident.Description.IsNullOrEmpty()) { strs.Add(" <!--" + ident.Description + "-->"); } if (!ident.IsList) { strs.Add(" <" + ident.Key + ">" + ident.Value + "</" + ident.Key + ">"); } else { strs.Add(" <" + ident.Key + ">"); foreach (string value in ident.Values) { strs.Add(" <Value>" + value + "</Value>"); } strs.Add(" </" + ident.Key + ">"); } } strs.Add("</" + GetType().Name + ">"); DirEx.CreateDir(Path.GetDirectoryName(filename)); File.WriteAllLines(filename, strs.ToArray(), Encoding.UTF8); }
/// <summary> /// 在指定目录下创建以年月分级的子目录,末尾包括\ /// </summary> /// <param name="dt">日期</param> /// <param name="path">文件夹</param> /// <param name="createIfNotExist">不存在是否创建</param> /// <returns>文件夹名</returns> public static string YearMonthFolder(this DateTime dt, string path, bool createIfNotExist = false) { if (path.IsNullOrEmpty()) { return(path); } string result = path.DealPath() + dt.YearString() + "\\" + dt.YearMonthString() + "\\"; if (createIfNotExist) { DirEx.CreateDir(result); } return(result); }
public static string SerializeToFile(object obj, string filename, Encoding encoding) { string jsonStr = Serialize(obj); try { DirEx.CreateDir(Path.GetDirectoryName(filename)); File.WriteAllText(filename, jsonStr, encoding); } catch (Exception e) { Console.WriteLine(e.Message); } return(jsonStr); }
/// <summary>保存到配置文件中去</summary> /// <param name="filename">文件名</param> public override void Save(string filename) { if (filename.IsNullOrWhiteSpace()) { filename = DirEx.CurrentDir() + ConfigFile; } if (filename.IsNullOrWhiteSpace()) { throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!"); } ConcurrentDictionary <string, Ident> idents = ConfigHelper.InitIdents(current); foreach (var ident in idents.Values) { if (ident.Section.IsNullOrEmpty()) { ident.Section = "Setup"; } } ConfigHelper.SaveConfigValue(Current, idents); List <string> strs = new List <string> { ";<!--" + Description + "-->", "" }; Dictionary <string, List <Ident> > listidents = new Dictionary <string, List <Ident> >(); foreach (var ident in idents.Values) { string section = ident.IsList ? ident.Section + "-" + ident.Key : ident.Section; if (!listidents.ContainsKey(section)) { listidents.Add(section, new List <Ident>()); } listidents[section].Add(ident); } foreach (var values in listidents) { strs.Add("[" + values.Key + "]"); SortedList <int, Ident> slist = new SortedList <int, Ident>(); foreach (var ident in values.Value) { slist.Add(ident.Index, ident); } foreach (var ident in slist.Values) { if (!ident.Description.IsNullOrEmpty()) { strs.Add(";<!--" + ident.Description + "-->"); } if (ident.IsList) { for (int i = 0; i < ident.Values.Count; i++) { strs.Add("Value" + i + "=" + ident.Values[i]); } } else { strs.Add(ident.Key + "=" + ident.Value); } } strs.Add(""); } listidents.Clear(); DirEx.CreateDir(Path.GetDirectoryName(filename)); File.WriteAllLines(filename, strs.ToArray(), IniBase.IniEncoding); }