C# FileSystemWatcher文件监控

对于一个需要管理本地文件的应用来说,监控文件的运行及修改状态十分重要,在运行中,我们如何知道文件的基本属性的变化?


C#为我们提供了一个文件监控类(FileSystemWatcher),它定义了几个文件操作的事件,当我们对文件操作时便会引起这些事件,通过订阅这些事件,并添加自己的处理程序可实现对文件的监控。

FileSystemWatcher的属性

属性说明
path设置要监控的目录或者文件
NotifyFilter它是NotifyFilters枚举值的组合,NotifyFilters枚举要监控的内容,它的枚举值是要监控的文件或文件夹属性。它的枚举值有,Attributes、CreationTime、DirectoryName、FileName、LastAccess等等。若有多个组合可使用二元运算符OR合并这些值。
Filter指定要监控的文件过滤,例如,*.txt

用FileSystemWatcher监控文件的Changed、Created、Deleted、Renamed事件,当文件引发上述事件后,便通过自定义的处理程序显示改变情况。(本例使用wpf应用程序)

  1. xaml代码:
<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="1*"/>
		<RowDefinition Height="1*"/>
		<RowDefinition Height="8*"/>
	</Grid.RowDefinitions>
	<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
		<TextBox x:Name="tb1" Height="30" Width="300" TextChanged="Tb1_TextChanged"/>
		<Button x:Name="btn1" Content="Open File" Margin="30 0 0 0" Click="Open_Click"/>
	</StackPanel>
	<Button x:Name="btn2" Grid.Row="1" Height="30" Width="200" Content="Watch!" 
		IsEnabled="False" Click="Watch_Click"/>
	<ListBox x:Name="lb1" Grid.Row="2" Margin="10"/>
</Grid>

效果截图
在这里插入图片描述

2.为ListBox添加消息

private void AddMessage(string message)
{
	Dispatcher.BeginInvoke(new Action(() => lb1.Items.Insert(0, message)));
}

3.实例化FileSystemWatcher并添加需监控属性的事件。

private FileSystemWatcher watcher;
public MainWindow()
{
	InitializeComponent();
	this.DataContext = this;
	watcher = new FileSystemWatcher();
	watcher.Changed += (s, e) => AddMessage(e.FullPath+" had changed");
	watcher.Deleted += (s, e) => AddMessage(e.FullPath + " had Deleted");
	watcher.Renamed += (s, e) => AddMessage(e.OldFullPath + " had renamed " + e.FullPath);
	watcher.Created += (s, e) => AddMessage(e.FullPath + "had created");
}

本例中订阅了四个事件,Changed文件修改事件,Deleted文件删除事件,Renamed重命名事件,Created文件创建事件。

4. 打开文件添加窗口

private void Open_Click(object sender, RoutedEventArgs e)
{
	OpenFileDialog openFile = new OpenFileDialog();
	openFile.Filter = "(*.*)|*.*";
	if(openFile.ShowDialog()==true)
	{
		this.tb1.Text = openFile.FileName;
	}
}

OpenFileDialog的ShowDialog会返回一个bool值,当选择好文件后会返回true,当取消此窗口后会返回false。

5.判断Watch按键是否可用

private void Tb1_TextChanged(object sender, TextChangedEventArgs e)
{
     btn2.IsEnabled = !string.IsNullOrEmpty(this.tb1.Text);
}

当路径框不为空时,watch按钮可用。

6.开始监控文件

private void Watch_Click(object sender, RoutedEventArgs e)
{
	watcher.Path = System.IO.Path.GetDirectoryName(this.tb1.Text);
	watcher.Filter = System.IO.Path.GetFileName(this.tb1.Text);
	watcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite
		| NotifyFilters.FileName | NotifyFilters.Deleted;
	AddMessage("Watching..." + this.tb1);
	watcher.EnableRaisingEvents = true;
}
  • EnableRaisingEvents属性设置为true时表示开始监控。
  • Path属性指定要监控的目录,通过Path的GetDirectoryName方法可以从路径字符串中获取路径。
  • Filter指定要过滤的文件,通过Path的GetFileName获取要筛选的文件,
    可指定Filter为(.)即为监控整个目录。

7.运行

创建文件
在这里插入图片描述
删除文件
在这里插入图片描述

感谢观看

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值