private void Start_Click(object sender, EventArgs e) { if (watcher != null) { watcher.Dispose(); watcher = null; } try { tracker = new SQLRec(); watcher = new Watcher(DirectoryTextBox.Text, comboBox1.Text, this, tracker); //If this successfully starts the watcher, then the stop button will be enabled this.StopWatcher.Enabled = true; toolStop.Enabled = true; StartWatcher.Enabled = false; toolStop.Enabled = false; DataWrite.Enabled = false; textBox1.AppendText("Started watching " + watcher.targetDir + " for " + watcher.filter + " @ " + DateTime.Now + Environment.NewLine); } catch (ArgumentNullException exc) { MessageBox.Show("One of the input args is null! " + exc); } catch (ArgumentException exc) { MessageBox.Show("Please enter a valid path. Empty paths are not valid."); } }
public Watcher(string targetD, string fil, MainWindow targetF1, SQLRec inp) { rec = inp; targetForm = targetF1; // Create a new FileSystemWatcher and set its properties. targetDir = targetD; if (fil == "") { fil = "*.*"; } FCS = new FileSystemWatcher(targetD, fil); /* Watch for changes in LastAccess and LastWrite times, and * the renaming of files or directories. */ FCS.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Setting filters // Add event handlers. FCS.Changed += new FileSystemEventHandler(OnChanged); FCS.Created += new FileSystemEventHandler(OnChanged); FCS.Deleted += new FileSystemEventHandler(OnChanged); FCS.Renamed += new RenamedEventHandler(OnRenamed); FCS.IncludeSubdirectories = true; // Begin watching. FCS.EnableRaisingEvents = true; }