private void InitWatcher() { // 文件监视 var fileName = FileName.GetBasePath(); var dir = Path.GetDirectoryName(fileName); if (Directory.Exists(dir)) { try { if (_watcher != null) { _watcher.TryDispose(); } _watcher = new FileSystemWatcher(dir, Path.GetFileName(fileName)) { NotifyFilter = NotifyFilters.LastWrite }; _watcher.Changed += Watch_Changed; _watcher.EnableRaisingEvents = true; } catch (Exception ex) { XTrace.WriteException(ex); } } }
private void DoRefresh(Object state) { if (_reading) { return; } var fileName = FileName.GetBasePath(); var fi = FileName.AsFile(); if (!fi.Exists) { return; } fi.Refresh(); if (_lastTime.Year > 2000 & fi.LastWriteTime <= _lastTime) { return; } _lastTime = fi.LastWriteTime; XTrace.WriteLine("配置文件改变,重新加载 {0}", fileName); _reading = true; try { var section = new ConfigSection { }; OnRead(fileName, section); Root = section; foreach (var item in _models) { base.Bind(item.Key, false, item.Value); } } catch (Exception ex) { XTrace.WriteException(ex); } finally { _reading = false; } }
private void Watch_Changed(Object sender, FileSystemEventArgs e) { if (e.ChangeType != WatcherChangeTypes.Changed || _reading) { return; } // 短时间内不要重复 if (_nextRead > DateTime.Now) { return; } _nextRead = DateTime.Now.AddSeconds(1); var fileName = FileName.GetBasePath(); if (File.Exists(fileName)) { XTrace.WriteLine("配置文件改变,重新加载 {0}", fileName); _reading = true; try { var section = new ConfigSection { }; OnRead(fileName, section); Root = section; foreach (var item in _models) { base.Bind(item.Key, false, item.Value); } } catch (Exception ex) { XTrace.WriteException(ex); } finally { _reading = false; } } }