C# FileSystemWatcher
using System.IO;
//file system watcher object.
private FileSystemWatcher watcher;
private delegate void UpdateWatchTextDelegate(string newText);
public void UpdateWatchText(string newText)
{
lblWatch.Text = newText;
}
public Form1()
{
InitializeComponent();
this.watcher = new FileSystemWatcher();
this.watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
this.watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
this.watcher.Changed += new FileSystemEventHandler(watcher_Changed);
this.watcher.Created += new FileSystemEventHandler(watcher_Created);
}
void watcher_Created(object sender, FileSystemEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File: {0} Created", e.FullPath);
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote create event to log");
}
catch (IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType.ToString());
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote change event to log");
}
catch (IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
void watcher_Renamed(object sender, RenamedEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File renamed from {0} to {1}", e.OldName, e.FullPath);
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote renamed event to log");
}
catch(IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
void watcher_Deleted(object sender, FileSystemEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File: {0} Deleted", e.FullPath);
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote delete event to log");
}
catch (IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
private void cmdBrowse_Click(object sender, EventArgs e)
{
if (FileDialog.ShowDialog() != DialogResult.Cancel)
{
txtLocation.Text = FileDialog.FileName;
cmdWatch.Enabled = true;
}
}
private void cmdWatch_Click(object sender, EventArgs e)
{
watcher.Path = Path.GetDirectoryName(txtLocation.Text);//监控路径(文件夹)
watcher.Filter = "*.*" ;//如果filter为文件名称则表示监控该文件,如果为*.txt则表示要监控指定目录当中的所有.txt文件
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.Size;
lblWatch.Text = "Watching " + txtLocation.Text;
//begin watching.
watcher.EnableRaisingEvents = true;
}
//file system watcher object.
private FileSystemWatcher watcher;
private delegate void UpdateWatchTextDelegate(string newText);
public void UpdateWatchText(string newText)
{
lblWatch.Text = newText;
}
public Form1()
{
InitializeComponent();
this.watcher = new FileSystemWatcher();
this.watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
this.watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
this.watcher.Changed += new FileSystemEventHandler(watcher_Changed);
this.watcher.Created += new FileSystemEventHandler(watcher_Created);
}
void watcher_Created(object sender, FileSystemEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File: {0} Created", e.FullPath);
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote create event to log");
}
catch (IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File: {0} {1}", e.FullPath, e.ChangeType.ToString());
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote change event to log");
}
catch (IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
void watcher_Renamed(object sender, RenamedEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File renamed from {0} to {1}", e.OldName, e.FullPath);
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote renamed event to log");
}
catch(IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
void watcher_Deleted(object sender, FileSystemEventArgs e)
{
//throw new NotImplementedException();
try
{
StreamWriter sw = new StreamWriter("c:\\Log.txt", true);
sw.WriteLine("File: {0} Deleted", e.FullPath);
sw.Close();
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote delete event to log");
}
catch (IOException)
{
this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
}
}
private void cmdBrowse_Click(object sender, EventArgs e)
{
if (FileDialog.ShowDialog() != DialogResult.Cancel)
{
txtLocation.Text = FileDialog.FileName;
cmdWatch.Enabled = true;
}
}
private void cmdWatch_Click(object sender, EventArgs e)
{
watcher.Path = Path.GetDirectoryName(txtLocation.Text);//监控路径(文件夹)
watcher.Filter = "*.*" ;//如果filter为文件名称则表示监控该文件,如果为*.txt则表示要监控指定目录当中的所有.txt文件
watcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.Size;
lblWatch.Text = "Watching " + txtLocation.Text;
//begin watching.
watcher.EnableRaisingEvents = true;
}
posted on 2009-01-07 23:18 ξσ Dicky σξ 阅读(1194) 评论(0) 编辑 收藏 举报
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步