C# 文件監視器簡單例子源碼
private void btn_FileWatcher_Click(object sender, EventArgs e)
{
FileSystemWatcher k8watcher = new FileSystemWatcher();
k8watcher.Path = "d:\\K8team";
k8watcher.IncludeSubdirectories = true;
k8watcher.EnableRaisingEvents = true;
k8watcher.SynchronizingObject = this;
k8watcher.Created += new FileSystemEventHandler(k8fileCreated);
k8watcher.Changed += new FileSystemEventHandler(k8fileChanged);
k8watcher.Renamed += new RenamedEventHandler(k8fileRenamed);
k8watcher.Deleted += new FileSystemEventHandler(k8fileDeleted);
}
public void k8fileCreated(object sender, FileSystemEventArgs e)
{
string log = string.Format(e.ChangeType + ":{0} --時間:{1}\r\n\r\n", e.FullPath, DateTime.Now.ToString());
txt_Relust.AppendText(log);
}
public void k8fileChanged(object sender, FileSystemEventArgs e)
{
string log = string.Format(e.ChangeType + ":{0} --時間:{1}\r\n\r\n", e.FullPath, DateTime.Now.ToString());
txt_Relust.AppendText(log);
}
public void k8fileDeleted(object sender, FileSystemEventArgs e)
{
string log = string.Format(e.ChangeType + ":{0} --時間:{1}\r\n\r\n", e.FullPath, DateTime.Now.ToString());
txt_Relust.AppendText(log);
}
public void k8fileRenamed(object sender,FileSystemEventArgs e)
{
string log = string.Format(e.ChangeType+":{0} --時間:{1}\r\n\r\n", e.FullPath, DateTime.Now.ToString());
txt_Relust.AppendText(log);
}