使用 FileSystemWatcher 监视指定目录中的更改。可监视指定目录中的文件或子目录的更改。该组件可以监视本地计算机、网络驱动器或远程计算机上的文件。
可监视目录或文件中的若干种更改。例如,可监视文件或目录的 Attributes、LastWrite 日期和时间或 Size 方面的更改。通过设置FileSystemWatcher.NotifyFilter属性。
可监视文件或目录的重命名、删除或创建。例如,若要监视文本文件的重命名,请将 Filter 属性设置为“*.txt”。注意 公共文件系统操作可能会引发多个事件。例如,将文件从一个目录移到另一个目录时,可能会引发若干 OnChanged 以及一些 OnCreated 和 OnDelete事件。移动文件是一个包含多个简单操作的复杂操作,因此会引发多个事件。同样,一些应用程序(如反病毒软件)可能导致被 FileSystemWatcher 检测到的附加文件系统事件。
注意FileSystemWatcher.NotifyFilter 属性设置的监视和事件(OnChanged,OnCreated,OnDelete等事件)是and的关系。
例子程序:
</>code
- public class Watcher
- {
- public static void Main()
- {
- string[] args = System.Environment.GetCommandLineArgs();
- // If a directory is not specified, exit program.
- if(args.Length != 2)
- {
- // Display the proper way to call the program.
- Console.WriteLine("Usage: Watcher.exe (directory)");
- return;
- }
- // Create a new FileSystemWatcher and set its properties.
- FileSystemWatcher watcher = new FileSystemWatcher();
- watcher.Path = args[1];
- /* Watch for changes in LastAccess and LastWrite times, and
- the renaming of files or directories. */
- watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
- | NotifyFilters.FileName | NotifyFilters.DirectoryName;
- // Only watch text files.
- watcher.Filter = "*.txt";
- // Add event handlers.
- watcher.Changed += new FileSystemEventHandler(OnChanged);
- watcher.Created += new FileSystemEventHandler(OnChanged);
- watcher.Deleted += new FileSystemEventHandler(OnChanged);
- watcher.Renamed += new RenamedEventHandler(OnRenamed);
- // Begin watching.
- watcher.EnableRaisingEvents = true;
- //Suspend the calling thread until the file has been created
- WaitForChangedResult cr= watcher.WaitForChanged(WatcherChangeTypes.Created);
- // Wait for the user to quit the program.
- Console.WriteLine("Press /'q/' to quit the sample.");
- while(Console.Read()!='q');
- }
- // Define the event handlers.
- private static void OnChanged(object source, FileSystemEventArgs e)
- {
- // Specify what is done when a file is changed, created, or deleted.
- Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
- }
- private static void OnRenamed(object source, RenamedEventArgs e)
- {
- // Specify what is done when a file is renamed.
- Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
- }
- }
但是,在实际使用过程中,由于需要监视的文件,必须在复制完成后才能进行后续工作,所以在监视时,需要不断进行判断,判断文件是否复制完成。所以修改
</>code
- private static void OnChanged(object source, FileSystemEventArgs e)
- {
- // Specify what is done when a file is changed, created, or deleted.
- Console.WriteLine("File: " + " " + e.ChangeType);
- string filepath = e.FullPath; //mypath + "//" + cr.Name;
- FileInfo fi = new FileInfo(filepath);
- if (!fi.Exists)
- {
- Console.WriteLine("file not exits!!");
- }
- Console.WriteLine(fi.IsReadOnly.ToString());
- //if (!fi.IsReadOnly)
- //{
- // fi.AppendText();
- //}
- HELLO: try
- {
- fi.OpenRead();
- }
- catch (IOException ex)
- {
- Console.WriteLine(ex.Message);
- Thread.Sleep(3000);
- goto HELLO;
- }
- OnRMChanged(e.FullPath);//对文件进行任意处理
- }
最后等复制完成再对文件进行操作如下
</>code
- private static void OnRMChanged(string mypath)
- {
- //对文件进行任意处理
- }
根据该文章改造成我自己的文件监控器,哈哈
FileSystemWatcher监听文件是否有被修改
作用:监听文件系统更改通知,并在目录或目录中的文件更改时引发事件。
需求:监听特定文件是否修改,然后做出相应的操作。
方法:
①利用一个线程,一直去查找该指定的文件是否有被修改,如果修改则操作特定步骤,否则继续查询。
缺点:占用CPU,要一直循环查找。
②利用.net里面的FileSystemWatcher来监听文件是否有被修改,如果有,则操作特定步骤。
代码:
①定义一个全局变量Watch
</>code
- FileSystemWatcher Watch;
②初始化该全局变量
</>code
- Watch = new FileSystemWatcher();
- Watch.Path = @"C:\Users\RAPOO\Desktop\123";
- Watch.Filter = "modlist.mod";
- Watch.NotifyFilter = NotifyFilters.LastWrite;
- Watch.IncludeSubdirectories = false;
- Watch.Changed += new FileSystemEventHandler(watch_changed);
- Watch.EnableRaisingEvents = true;
③相应修改事件
</>code
- private void watch_changed(object source, FileSystemEventArgs e)
- { if (Watch != null)
- { try
- {
- Watch.EnableRaisingEvents = false;
- MessageBox.Show("改变!!!");
- } finally
- {
- Watch.EnableRaisingEvents = true;
- }
- }
- }
注意:
1、代码只添加修改事件,还有重命名、删除、新增事件。
2、在修改事件里面,需要将事件监控先重置为false,待执行结束后再修改为true。目的是解决修改事件执行两次的BUG。
下图为MSDN上注释。
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛