/// <summary> /// 文件改变 /// </summary> /// <param name="render"></param> /// <param name="e"></param> private void OnFileChanged(object render, FileSystemEventArgs e) { if (!FileHelper.IsFileLocked(e.FullPath)) { if (render is FileSystemWatcher watcher) { watcher.EnableRaisingEvents = false; if (e.ChangeType == WatcherChangeTypes.Changed) { JsonConfigurationProvider.Set(e.Name, File.ReadAllText(e.FullPath)); } watcher.EnableRaisingEvents = true; } } }
public void Start(JsonConfigurationPath conf) { var watcher = new FileSystemWatcher(); watcher.IncludeSubdirectories = false; watcher.Path = conf.Path; watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.Size; watcher.Filter = conf.Name; watcher.EnableRaisingEvents = true; Observable.FromEventPattern <FileSystemEventArgs>(watcher, "Changed") .Throttle(TimeSpan.FromSeconds(1)) .Subscribe(e => { if (!FileHelper.IsFileLocked(e.EventArgs.FullPath) && e.EventArgs.ChangeType == WatcherChangeTypes.Changed) { JsonConfigurationProvider.Set(e.EventArgs.Name, File.ReadAllText(e.EventArgs.FullPath)); } }, ex => { /* 处理异常 */ }, () => { /* 处理完成 */ } ); }
/// <summary> /// 根据配置文件的键获取值(数组) /// </summary> /// <param name="key">键</param> /// <returns></returns> public static IEnumerable <string> Gets(string key) { return(JsonConfigurationProvider.GetSections(JsonFileName, key)); }
/// <summary> /// 根据配置文件的键获取值(字符串) /// </summary> /// <param name="key">键</param> /// <returns></returns> public static string Get(string key) { return(JsonConfigurationProvider.GetSection(JsonFileName, key)); }
/// <summary> /// 根据配置文件的键获取值(对象集合) /// </summary> /// <param name="section"></param> /// <returns></returns> public static IEnumerable <T> GetSections <T>(string section) where T : class { return(JsonConfigurationProvider.GetSections <T>(JsonFileName, section)); }
/// <summary> /// 根据配置文件的键获取值(对象) /// </summary> /// <param name="section"></param> /// <returns></returns> public static T GetSectionNext <T>(string section) where T : class { return(JsonConfigurationProvider.GetSectionNext <T>(JsonFileName, section, _environment)); }
/// <summary> /// 根据配置文件的键获取值(对象) /// </summary> /// <param name="section"></param> /// <returns></returns> public static T GetSection <T>(string section) where T : class { return(JsonConfigurationProvider.GetSection <T>(JsonFileName, section)); }