C# 未激发FileWatcher创建事件

C# 未激发FileWatcher创建事件,c#,C#,我编写了一个filewatcher服务,用于递归监视目录,如果目录中有.txt文件内容更改或新创建的.txt文件,它会将该文件复制到中心文件夹。但是我的程序遇到了一些问题 每当.txt文件内容发生更改时,我的程序都运行良好。但是,当目录中创建了一个新的.txt文件时,创建的事件从未触发。下面是我的代码,你能帮我解决这个问题吗?提前谢谢 using System; using System.Collections.Generic; using System.ComponentModel; usin

我编写了一个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()
        {

        }
    }
}

我想,你的过滤器有问题。必须添加NotifyFilters.FileName才能获取创建的事件。我可以在一个小的解决方案中重新介绍它。

您正在写入
C:\
的根目录,除非您的程序以管理员身份运行,否则您的日志文件将写入
C:\Users\UserName\AppData\Local\VirtualStore\log.txt
。我认为我的程序将内容写入C:\log.txt没有问题,因为当文件内容发生更改时,我看到它确实将更改事件写入了C:\log.txt。NotifyFilters.FileName的具体内容会影响什么?我想它只会影响已更改的事件,而不会影响已创建的事件。NotifyFilters.FileName是否意味着只要文件名发生更改,就会触发某些事件?然后重命名事件与它的效果相同。可能更改是从null到“whatever.txt”。