当文件修改后,触发其他操作,例如删除另一个文件夹下的文件等
-
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
-
- namespace ConsoleApp6FileSystemWatcher
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- string path = @"C:\Users\admin\Desktop\测试文件夹";
- MonitorDirectory(path, "*.*");
- Console.ReadKey();
- Console.WriteLine("按q退出!");
- while (Console.Read() != 'q') ;
- }
- private static void runProgress(string exename, string para)
- {
- Process p = new Process();
- p.StartInfo.FileName = exename;
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.CreateNoWindow = true;
- p.Start();
- p.StandardInput.WriteLine(para); //向cmd窗口发送输入信息
- p.StandardInput.WriteLine("exit");//要退出
- p.StandardInput.AutoFlush = true; //提交
- p.WaitForExit();
- //p.Close();
- }
- private static void MonitorDirectory(string path, string filter)
- {
- FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
- //设置要监视的目录的路径。
- fileSystemWatcher.Path = path;
- //设置文件的哪些属性的变动会触发 Changed事件,同时监控多个属性变动可以按“或”组合。
- //Attributes – 文件或文件夹的属性。
- //CreationTime – 文件或文件夹的创建时间。
- //DirectoryName – 目录名。(常用)
- //FileName – 文件名。 (常用)
- //LastAccess – 文件或文件夹上一次打开的日期。
- //LastWrite – 上一次向文件或文件夹写入内容的日期。
- //Security – 文件或文件夹的安全设置。
- //Size – 文件或文件夹的大小。 (常用)
- fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess
- | NotifyFilters.LastWrite
- | NotifyFilters.FileName
- | NotifyFilters.DirectoryName;
- //文件类型,支持通配符,“*.txt”只监视文本文件
- fileSystemWatcher.Filter = filter; // 监控的文件格式
- fileSystemWatcher.IncludeSubdirectories = true; // 监控子目录
- //Changed: 当文件或者文件夹已经完成修改时触发此事件
- //Created:当文件或者文件夹已经成功创建触发此事件
- //Deleted:当文件或者文件夹已经成功删除触发此事件
- //Error:当变更的过程中发生错误触发此事件。
- //Renamed:当文件或者文件夹已经成功被重命名时触发此事件
- fileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);
- fileSystemWatcher.Created += new FileSystemEventHandler(OnCreated);
- fileSystemWatcher.Renamed += new RenamedEventHandler(OnRenamed);
- fileSystemWatcher.Deleted += new FileSystemEventHandler(OnDeleted);
- //表示当前的路径正式开始被监控,一旦监控的路径出现变更,FileSystemWatcher 中的指定事件将会被触发。
- fileSystemWatcher.EnableRaisingEvents = true;
- }
- private static void OnCreated(object source, FileSystemEventArgs e)
- {
- Console.WriteLine("检测到文件创建");
- Console.WriteLine("文件操作类型"+ e.ChangeType);
- Console.WriteLine("文件路径" + e.FullPath);
- Console.WriteLine("文件名称" + e.Name);
- }
-
- private static void OnChanged(object source, FileSystemEventArgs e)
- {
- Console.WriteLine("检测到文件修改");
- Console.WriteLine("文件操作类型" + e.ChangeType);
- Console.WriteLine("文件路径" + e.FullPath);
- Console.WriteLine("文件名称" + e.Name);
- }
-
- private static void OnDeleted(object source, FileSystemEventArgs e)
- {
- Console.WriteLine("检测到文件删除");
- Console.WriteLine("文件操作类型" + e.ChangeType);
- Console.WriteLine("文件路径" + e.FullPath);
- Console.WriteLine("文件名称" + e.Name);
- }
-
- private static void OnRenamed(object source, FileSystemEventArgs e)
- {
- Console.WriteLine("检测到文件重命名");
- Console.WriteLine("文件操作类型" + e.ChangeType);
- Console.WriteLine("文件路径" + e.FullPath);
- Console.WriteLine("文件名称" + e.Name);
- }
- }
- }
