繁体   English   中英

FileWatcher创建事件未触发

[英]FileWatcher creation event not fired

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

        }
    }
}

我认为您的NotifyFilter中有问题。 您必须添加NotifyFilters.FileName才能获取创建事件。 我可以用一个小的解决方案来解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM