-
C#教程之c#使用filesystemwatcher监视文件系统的变化
#region 监视文件夹的变化
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "f:\\";
watcher.NotifyFilter =//被监控的方面
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
// 订阅一些事件,当它被触发时(.net(windows)底层触发它,我们不用管),执行我们的方法
watcher.Changed += (object source, FileSystemEventArgs e) =>
{
Console.WriteLine("文件{0}已经被修改,修改类型{1}", e.FullPath, e.ChangeType.ToString());
};
watcher.Created += (object source, FileSystemEventArgs e) =>
{
Console.WriteLine("文件{0}被建立", e.FullPath);
};
watcher.Deleted += (object source, FileSystemEventArgs e) =>
{
Console.WriteLine("文件{0}已经被删除", e.FullPath);
};
watcher.Renamed += (object source, RenamedEventArgs e) =>
{
Console.WriteLine("文件{0}的名称已经从{1}变成了{2}", e.OldFullPath, e.OldName, e.Name);
};
// 为true表示开启FileSystemWatcher组件,反之我们的监控将不启作用
watcher.EnableRaisingEvents = true;
#endregion
另外,告诉大家一个 xor异或运算的使用技巧,就是它可以在不引入第三个变量的情况下,交替两个变量的值,你的变量可以是数值,也可以是字符,如果是字符,我们需要使用它的hashcode值进行xor运算。
#region xor两个变量交换
int a = 2;
int b = 3;
Console.WriteLine("a={0}", a);
Console.WriteLine("b={0}", b);
a = a ^ b ^ (b = a);
Console.WriteLine("a={0}", a);
Console.WriteLine("b={0}", b);
#endregion