C# FileSystemWatcher 如何监控Changed事件到底是文件大小改变还是文件属性变化还是访问时间变化呢?

解决方案 »

  1.   

    FileSystemWatcher.NotifyFilter属性可用来控制。
    NotifyFilter属性为可组合 NotifyFilters 枚举的成员,以同时监视多种类型的更改。例如,可监视文件大小方面的更改和 LastWrite 时间方面的更改。只要文件或文件夹大小有变化或者如果文件或文件夹的 LastWrite 时间有变化,就会引发事件。
    public class Watcher
    {    public static void Main()
        {
        Run();    }    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
        public static void Run()
        {
            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;        // 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);
        }
    }
      

  2.   

    楼上的大哥,我是问如何确定到底是引发了Changed事件里的哪种情况...
    我也知道可以设置多种监控
      

  3.   

    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); 

    其中:e.ChangeType就是你需要的,ChangeType 地取值为WatcherChangeTypes 值之一,表示在文件系统中检测到的更改种类。
    参考代码:
    if(e.ChangeType == WatcherChangeTypes.Changed)
    {...}
      

  4.   

    大哥,你还是没弄明白我要问的是什么,
    e.ChangeType里只有4种变化 其中一个就是Changed
    我是知道发生了Changed事件
    可是由什么引起的呢? 改文件大小了? 访问时间修改了?
    还是文件属性修改了?
      

  5.   

    正解,每一个Watcher监视一种变化,参考代码:
    FileSystemWatcher watcherSize = new FileSystemWatcher();
    watcherSize.Changed += new FileSystemEventHandler(watcherSize_Changed);
    watcherSize.NotifyFilter = NotifyFilters.Size;
    FileSystemWatcher watcherLastAccess = new FileSystemWatcher();
    watcherLastAccess.NotifyFilter = NotifyFilters.LastAccess;
    watcherLastAccess.Changed += new FileSystemEventHandler(watcherLastAccess_Changed);
      

  6.   

    使用watcherSize.NotifyFilter来控制监视的类型,并挂接对应的Changed事件;
    该事件响应函数的参数:e.ChangeType就是对应的变化了
    if(e.ChangeType == WatcherChangeTypes.Changed) 
    {...}