Created
January 31, 2010 20:50
-
-
Save robertgreiner/291234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.IO; | |
namespace FolderWatcher { | |
public partial class FolderWatcher : Form { | |
private FileSystemWatcher watcher = new FileSystemWatcher(); | |
private string logMessage = ""; | |
public FolderWatcher() { | |
InitializeComponent(); | |
} | |
private void FolderWatcher_Load(object sender, EventArgs e) { | |
this.boxFolder.Text = @"D:\test"; | |
this.startMonitoring(); | |
} | |
private void startMonitoring() { | |
this.watcher.Path = boxFolder.Text; | |
this.watcher.Filter = ""; | |
this.watcher.IncludeSubdirectories = true; | |
this.watcher.Renamed += new RenamedEventHandler(renamed); | |
this.watcher.Deleted += new FileSystemEventHandler(changed); | |
this.watcher.Changed += new FileSystemEventHandler(changed); | |
this.watcher.Created += new FileSystemEventHandler(changed); | |
this.watcher.EnableRaisingEvents = true; | |
} | |
private void renamed(object sender, RenamedEventArgs e) { | |
this.logMessage = new StringBuilder(DateTime.Now + ": " + e.ChangeType + " " + e.FullPath + "\n").ToString(); | |
if (this.InvokeRequired) { | |
this.Invoke(new MethodInvoker(writeLog)); | |
} else { | |
writeLog(); | |
} | |
} | |
private void changed(object sender, FileSystemEventArgs e) { | |
this.logMessage = new StringBuilder(DateTime.Now + ": " + e.ChangeType + " " + e.FullPath + "\n").ToString(); | |
if (this.InvokeRequired) { | |
this.Invoke(new MethodInvoker(writeLog)); | |
} else { | |
writeLog(); | |
} | |
} | |
private void FolderWatcher_Resize(object sender, EventArgs e) { | |
if (FormWindowState.Minimized == WindowState) { | |
this.Hide(); | |
this.minIcon.Visible = true; | |
} | |
} | |
private void minIcon_MouseDoubleClick(object sender, MouseEventArgs e) { | |
this.Show(); | |
this.WindowState = FormWindowState.Normal; | |
this.minIcon.Visible = false; | |
} | |
private void btnBrowse_Click(object sender, EventArgs e) { | |
this.folderBrowserDialog.ShowDialog(); | |
this.boxFolder.Text = folderBrowserDialog.SelectedPath; | |
this.setPath(boxFolder.Text); | |
} | |
private void setPath(string path) { | |
if (Directory.Exists(path)) { | |
this.watcher.EnableRaisingEvents = false; | |
this.watcher.Path = path; | |
this.watcher.EnableRaisingEvents = true; | |
this.boxInfo.AppendText("Changed Directory: " + path + "\n"); | |
} else { | |
this.boxInfo.AppendText("ERROR: " + path + " does not exist.\n"); | |
} | |
} | |
private void writeLog() { | |
this.boxInfo.AppendText(this.logMessage); | |
} | |
private void boxFolder_Leave(object sender, EventArgs e) { | |
this.setPath(boxFolder.Text); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment