显示界面
实例化FileSystemWatcher
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
初始化FileSystemWatcher参数及创建相关事件方法
private void InitFileSystemWatcher()
{
fileSystemWatcher.Filter = "*.*";
fileSystemWatcher.Path = System.AppDomain.CurrentDomain.BaseDirectory;
tb_Path.Text = fileSystemWatcher.Path;
fileSystemWatcher.EnableRaisingEvents = true;
fileSystemWatcher.Changed += new FileSystemEventHandler(FileSystemWatcher_Changed);
fileSystemWatcher.Created += new FileSystemEventHandler(FileSystemWatcher_Created);
fileSystemWatcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_Deleted);
fileSystemWatcher.Renamed += new RenamedEventHandler(FileSystemWatcher_Renamed);
fileSystemWatcher.Error += new ErrorEventHandler(FileSystemWatcher_Error);
}
相关处理事件
/// <summary>
/// 错误事件的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileSystemWatcher_Error(object sender, ErrorEventArgs e)
{
if (listBox_Info.InvokeRequired)
{
Invoke(new Action(new Action(() =>
{
listBox_Info.Items.Add(string.Format("【{0}】错误!", e.ToString()));
})));
}
else
{
listBox_Info.Items.Add(string.Format("【{0}】错误!", e.ToString()));
}
}
/// <summary>
/// 重命名方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
if (listBox_Info.InvokeRequired)
{
Invoke(new Action(new Action(() =>
{
listBox_Info.Items.Add(string.Format("文件【{0}】被换名为:【{1}】", e.OldName, e.Name));
})));
}
else
{
listBox_Info.Items.Add(string.Format("文件【{0}】被换名为:【{1}】", e.OldName, e.Name));
}
}
/// <summary>
/// 发生删除事件时的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
if (listBox_Info.InvokeRequired)
{
Invoke(new Action(new Action(() =>
{
listBox_Info.Items.Add(string.Format("【{0}】被删除", e.Name));
})));
}
else
{
listBox_Info.Items.Add(string.Format("【{0}】被删除", e.Name));
}
}
/// <summary>
/// 创建新文件时的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
if (listBox_Info.InvokeRequired)
{
Invoke(new Action(new Action(() =>
{
listBox_Info.Items.Add(string.Format("有新文件创建,文件名为【{0}】", e.Name));
})));
}
else
{
listBox_Info.Items.Add(string.Format("有新文件创建,文件名为【{0}】", e.Name));
}
}
/// <summary>
/// 新建文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
if (listBox_Info.InvokeRequired)
{
Invoke(new Action(new Action(() =>
{
listBox_Info.Items.Add(string.Format("有新文件夹,文件夹名为【{0}】", e.Name));
})));
}
else
{
listBox_Info.Items.Add(string.Format("有新文件夹,文件夹名为【{0}】", e.Name));
}
}
/// <summary>
/// 修改为APP运行路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_DefPath_Click(object sender, EventArgs e)
{
fileSystemWatcher.Path = System.AppDomain.CurrentDomain.BaseDirectory;
tb_Path.Text = fileSystemWatcher.Path;
}
/// <summary>
/// 指定输入的路径
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Monitor_Click(object sender, EventArgs e)
{
//包含空格的路径 或 路径+文件名(且\后不能有空格)
if (Regex.IsMatch(tb_Path.Text, @"[a-zA-Z]:(\\[\w\u4e00-\u9fa5]+[\w\u4e00-\u9fa5\s]*)+"))
{
fileSystemWatcher.Path = tb_Path.Text;
}
}