/// <summary>加载指定配置文件</summary> /// <param name="filename">文件名</param> /// <returns>结果</returns> public override bool Load(string filename) { if (filename.IsNullOrWhiteSpace()) { filename = DirEx.CurrentDir() + ConfigFile; } if (filename.IsNullOrWhiteSpace()) { throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!"); } if (!File.Exists(filename)) { return(false); } try { ConcurrentDictionary <string, Ident> idents = ConfigHelper.InitIdents(current); foreach (var ident in idents.Values) { if (ident.Section.IsNullOrEmpty()) { ident.Section = "Setup"; } } IniFile ini = new IniFile(filename); foreach (var ident in idents.Values) { if (ident.IsList) { ident.Values.Clear(); NameValueCollection list = new NameValueCollection(); ini.GetSectionValues(ident.Section + "-" + ident.Key, list); foreach (var pair in list) { ident.Values.Add(ini.Read(ident.Section + "-" + ident.Key, pair.ToString(), "")); } } else { ident.Value = ini.Read(ident.Section, ident.Key, ""); } } ConfigHelper.LoadConfigValue(current, idents); return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
/// <summary>加载指定配置文件</summary> /// <param name="filename">文件名</param> /// <returns>结果</returns> public override bool Load(string filename) { if (filename.IsNullOrWhiteSpace()) { filename = DirEx.CurrentDir() + ConfigFile; } if (filename.IsNullOrWhiteSpace()) { throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径!"); } if (!File.Exists(filename)) { return(false); } try { ConcurrentDictionary <string, Ident> idents = ConfigHelper.InitIdents(current); XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlElement root = doc.DocumentElement; //获取根节点 foreach (Ident ident in idents.Values) { if (root != null) { var elements = root.GetElementsByTagName(ident.Key); if (elements.Count == 1) { if (ident.IsList) { ident.Values.Clear(); foreach (XmlNode node in elements[0].ChildNodes) { ident.Values.Add(node.InnerText); } } else { ident.Value = elements[0].InnerText; } } } } ConfigHelper.LoadConfigValue(current, idents); return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
/// <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="input">数组</param> /// <returns>结果</returns> public static byte[] Decompress(byte[] input) { string zipfile = FileEx.TempFileName(); File.WriteAllBytes(zipfile, input); string unzipDir = DirEx.TempRandomPath(); UnZipFile(zipfile, unzipDir); string[] fall = Directory.GetFiles(unzipDir, "*.*", SearchOption.TopDirectoryOnly); string unzipfile = fall[0]; byte[] bts = File.ReadAllBytes(unzipfile); FileEx.TryDelete(unzipfile); FileEx.TryDelete(zipfile); DirEx.TryDelete(unzipDir); return(bts); }
private static bool SeventZPrcess(string arguments) { Process process = new Process(); process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏压缩窗口 process.StartInfo.FileName = DirEx.CurrentDir() + "7z.exe"; process.StartInfo.CreateNoWindow = false; process.StartInfo.Arguments = arguments; process.Start(); process.WaitForExit(); if (process.HasExited) { int iExitCode = process.ExitCode; process.Close(); if (iExitCode != 0 && iExitCode != 1) { return(false); } } return(true); }
/// <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); }