未触发FileWatcher创建事件

时间:2014-03-23 18:26:52

标签: c#

我编写了一个用于递归监视目录的filewatcher服务,如果目录中有.txt文件内容更改或新创建的.txt文件,它会将文件复制到中央文件夹。但我在我的计划中遇到了一些问题。

每当.txt文件内容发生变化时,我的程序运行良好。但是,当在目录中创建新的.txt文件时,创建的事件永远不会被触发。以下是我的代码,你能帮我解决这个问题吗?提前谢谢!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FileWatcher
{
    public partial class FileWatcher : ServiceBase
    {
        private string des = @"c:\b";
        private FileSystemWatcher watcher;
        public FileWatcher()
        {
            InitializeComponent();
            watcher = new FileSystemWatcher();
        }

        protected override void OnStart(string[] args)
        {


            watcher.Path = @"C:\a";
            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite;

            // Only watch text files.
            watcher.Filter = "*.txt";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnCreated);


            // Begin watching.
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;
        }
        private void OnChanged(object sender, FileSystemEventArgs e) {

            string fileName = Path.GetFileName(e.Name);
            string destFile = System.IO.Path.Combine(des, fileName);
            using (System.IO.StreamWriter f = new System.IO.StreamWriter(@"C:\log.txt", true))
            {
                f.WriteLine(fileName);
            }


            using (System.IO.StreamWriter f = new System.IO.StreamWriter(@"C:\log.txt", true))
            {
                f.WriteLine(destFile);
            }

            System.IO.File.Copy(e.FullPath, destFile, true);

        }

        private void OnCreated(object sender, FileSystemEventArgs e) {
            using (System.IO.StreamWriter f = new System.IO.StreamWriter(@"C:\log.txt", true))
            {
                f.WriteLine("create new");
            }

            FileAttributes attr = File.GetAttributes(e.FullPath);
            //detect whether its a directory or file
            if ((attr & FileAttributes.Directory) == FileAttributes.Directory) {
                foreach (string file in Directory.GetFiles(e.FullPath))
                {
                    var eventArgs = new FileSystemEventArgs(
                        WatcherChangeTypes.Created,
                        Path.GetDirectoryName(file),
                        Path.GetFileName(file));
                    OnCreated(sender, eventArgs);
                }


            }
            else {
                string fileName = e.Name;
                string destFile = System.IO.Path.Combine(des, fileName);
                System.IO.File.Copy(e.FullPath, destFile, true);
            }


        }


        protected override void OnStop()
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为,你的NotifyFilter存在问题。您必须添加NotifyFilters.FileName才能获取created-event。我可以用一个小小的解决方案来重新定位它。