static void Main(string[] args) { if (args.Length > 0 && (args[0].Equals("gui", StringComparison.CurrentCultureIgnoreCase) || args[0].Equals("g", StringComparison.CurrentCultureIgnoreCase))) { ConfigurationListWindow mainWindow = new ConfigurationListWindow(); Application.Run(mainWindow); return; } FolderManager folder = new FolderManager(); FolderConfigurationManager config = new FolderConfigurationManager(); foreach (FolderConfiguration item in config.GetCurrentConfigurations()) { folder.AddToMonitor(item); } config.OnConfigChanged((path, action) => { try { Console.WriteLine(action.ToString() + "> " + path); switch (action) { case System.IO.WatcherChangeTypes.Created: folder.AddToMonitor(FolderConfiguration.Deserialize(path)); break; case System.IO.WatcherChangeTypes.Deleted: folder.RemoveMonitor(path); break; case System.IO.WatcherChangeTypes.Changed: folder.ChangeMonitor(path); break; case System.IO.WatcherChangeTypes.Renamed: break; case System.IO.WatcherChangeTypes.All: break; default: break; } } catch (Exception ex) { string contentLog = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " $ " + ex.Message + Environment.NewLine; System.IO.File.AppendAllText("Log" + DateTime.Now.ToString("yyyy/MM/dd") + ".txt", contentLog); } }); Console.ReadLine(); }
private void ConfigurationListWindow_Load(object sender, EventArgs e) { FolderConfigurationManager folders = new FolderConfigurationManager(); grdConfigurations.DataSource = folders.GetCurrentConfigurations(); folders.OnConfigChanged((file, action) => { switch (action) { case System.IO.WatcherChangeTypes.Created: List <FolderConfiguration> obj = ((List <FolderConfiguration>)grdConfigurations.DataSource); obj.Add(FolderConfiguration.Deserialize(file)); grdConfigurations.Invoke(new Action(() => { grdConfigurations.DataSource = null; grdConfigurations.DataSource = obj; })); break; case System.IO.WatcherChangeTypes.Deleted: List <FolderConfiguration> objD = ((List <FolderConfiguration>)grdConfigurations.DataSource); grdConfigurations.Invoke(new Action(() => { grdConfigurations.DataSource = null; grdConfigurations.DataSource = objD.Where(q => q.FullPath != file).ToList(); })); break; case System.IO.WatcherChangeTypes.Changed: if (((List <FolderConfiguration>)grdConfigurations.DataSource).Count(q => q.FullPath == file) > 0) { List <FolderConfiguration> objC = ((List <FolderConfiguration>)grdConfigurations.DataSource); int index = objC.IndexOf(objC.First(q => q.FullPath == file)); objC[index] = FolderConfiguration.Deserialize(file); grdConfigurations.Invoke(new Action(() => { grdConfigurations.DataSource = null; grdConfigurations.DataSource = objC; })); } break; case System.IO.WatcherChangeTypes.Renamed: grdConfigurations.Invoke(new Action(() => { grdConfigurations.DataSource = null; grdConfigurations.DataSource = folders.GetCurrentConfigurations(); })); break; case System.IO.WatcherChangeTypes.All: break; default: break; } }); }