void Initialize(string file) { if (file.IsEmpty()) { var exefile = EnvH.EntryPath; var file1 = exefile + ".conifg"; var file2 = Path.GetDirectoryName(exefile) + "\\AppConifg.conifg"; if (File.Exists(file1)) { file = file1; } else if (File.Exists(file2)) { file = file2; } } this.xmlFile = FSH.GetPath(file); this.xmlFileUpper = this.xmlFile.ToUpper(); var document = new XmlDocument(); var retry = 3; start: try { if (!file.IsEmpty() && File.Exists(file)) { var xmlcontent = ReadConfigTxt(file, false); document.LoadXml(xmlcontent); appSettings = XmlHelper.GetChild(document, "appSettings"); Debug.WriteLine("成功加载配置文件" + file); } } catch (Exception ex) { if (retry-- > 0) { TredH.SleepAsync(1000); goto start; } } if (appSettings != null) { conifgNode = appSettings.ParentNode; } else { if (document.ChildNodes.Count == 0) { document.AppendChild(document.CreateNode(XmlNodeType.XmlDeclaration, null, null)); } conifgNode = XmlHelper.GetChild(document, "conifiguration"); if (conifgNode == null) { conifgNode = document.CreateNode(XmlNodeType.Element, "conifiguration", null); } document.AppendChild(conifgNode); appSettings = document.CreateNode(XmlNodeType.Element, "appSettings", null); document.AppendChild(appSettings); } }
private void InitFileSystemWatcher() { if (!TredH.GetFlag(ref watcherFlag)) { try { FSH.WatchFile(this.xmlFile, (e) => { if (e.FullPath.ToUpper() == this.xmlFileUpper) { Interlocked.Increment(ref changeCount); ThreadHelper.InvokeOnce(RaiseConifgChanged, SMath.Second); } else { if (e.ChangeType == WatcherChangeTypes.Renamed) { if (((RenamedEventArgs)e).OldFullPath.ToUpper() == this.xmlFileUpper) { Interlocked.Increment(ref changeCount); TredH.InvokeOnce(RaiseConifgChanged, SMath.Second); } } } }); } catch (Exception ex) { } } }
/// <summary> /// 写配置文件 /// </summary> /// <param name="file"></param> /// <param name="text"></param> public static void SaveConfigTxt(string file, string text) { lock (configtxt) { var upptxt = file.ToUpper().Replace("\\", "/"); configtxt[upptxt] = text; FSH.WriteAllText(file, text, Encoding.UTF8); } }
/// <summary> /// 读取配置文件 /// </summary> /// <param name="file"></param> /// <param name="force"></param> /// <returns></returns> public static string ReadConfigTxt(string file, bool force) { lock (configtxt) { var uptxt = file.ToUpper().Replace("\\", "/"); if (force || !configtxt.ContainsKey(uptxt)) { var text = FSH.ReadAllText(uptxt); configtxt[uptxt] = text; return(text); } return(configtxt[uptxt]); } }
private void InitFileSystemWatcher() { if (TredH.GetFlag(ref watchflag)) { FSH.WatchFile(this.jsonfile, (e) => { if (e.FullPath.ToUpper() == this.jsonfileUpper) { Interlocked.Increment(ref changecount); } else { if (e.ChangeType == WatcherChangeTypes.Renamed) { if (((RenamedEventArgs)e).OldFullPath.ToUpper() == this.jsonfileUpper) { Interlocked.Increment(ref changecount); } } } }); } }