FileSystemWatcher的不加,当文件被复制或移动到文件夹不加、文件夹、文件、FileSystemWatcher

2023-09-05 23:59:14 作者:长情又很酷

可能重复:   Detecting移动使用FileSystemWatcher的文件

我一直在寻找一个解决方案,看目录,并通知我的应用程序,每当一个新的文件移动到该目录。显而易见的解决办法是使用.NET的FileSystemWatcher类。

但问题是,它提出了在文件夹中删除,创建一个新的文件中的事件/但不引发事件,当一个文件被移动/复制到文件夹中。

谁能告诉我这可能是这种行为的原因。

我的code是

 静态无效的主要(字串[] args)
    {
        跑();
    }

    [PermissionSet中(SecurityAction.Demand,名称=FullTrust的)
    公共静态无效的run()
    {
        //创建一个新的FileSystemWatcher的,并设置其属性。
        FileSystemWatcher的观察家=新FileSystemWatcher的();
        watcher.Path = @D:\新建文件夹;
        / *留意在LastAccess变化和LastWrite倍,
           文件或目录的重新命名。 * /
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        //只有看的文本文件。
        watcher.Filter =* .TXT;

        //添加事件处理程序。
        watcher.Changed + =新FileSystemEventHandler(调用onChanged);
        watcher.Created + =新FileSystemEventHandler(调用onChanged);
        watcher.Deleted + =新FileSystemEventHandler(调用onChanged);
        watcher.Renamed + =新RenamedEventHandler(OnRenamed);

        //开始观望。
        watcher.EnableRaisingEvents = TRUE;

        //等待退出该程序的用户。
        Console.WriteLine(preSS \'Q \'退出样本。);
        而(Console.Read()=Q!);
    }

    //定义事件处理程序。
    私有静态无效调用onChanged(对象源,FileSystemEventArgs E)
    {
        //指定做了什么时,文件被更改,创建或删除。
        Console.WriteLine(文件:+ e.FullPath ++ e.ChangeType);
    }

    私有静态无效OnRenamed(对象源,RenamedEventArgs E)
    {
        //指定什么,当一个文件被重命名完成。
        Console.WriteLine(文件:{0}重命名为{1},e.OldFullPath,e.FullPath);
    }
 

解决方案

我在我家应用程序的一个使用FileSystemWatcher的。但FileSystemWatcher的没有任何移动或复制检测事件按我的知识。

根据MSDN

  

复制和移动文件夹

     

操作系统和FileSystemWatcher的对象除preT一   剪切和粘贴操作或移动操作为重命名操作的文件夹   及其内容。如果剪切和文件的文件夹粘贴到一个   文件夹只被注视下,FileSystemWatcher的对象报告   文件夹作为新的,但不是它的内容,因为它们是基本上只   重命名。

有关更多信息,请点击这里。

我所做的就是监测父文件夹和子文件夹,并记录在它的每一个变化。 要包括子目录我用下面的属性。

  watcher.IncludeSubdirectories = TRUE;
 
怎样加快移动硬盘复制文件速度

一些谷歌搜索建议使用定时器来检测更改。但我不知道它是多么有效。

Possible Duplicate: Detecting moved files using FileSystemWatcher

I was looking for a solution to watch a directory and notify my app whenever a new file is moved into the directory. The Obvious solution was to use FileSystemWatcher class of the .NET.

But the problem is , it raises a event a new file is created/deleted in the folder but does not raises event when a file is moved/copied into the folder.

Can anyone tell me what could be the cause of such behavior.

my code is

static void Main(string[] args)
    {
        Run();
    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public static void Run()
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"D:\New folder";
        /* 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);
    }

解决方案

I used FileSystemWatcher in one of my Home application. But FileSystemWatcher do not have any move or copy detection events as per my knowledge.

As per MSDN

Copying and moving folders

The operating system and FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents. If you cut and paste a folder with files into a folder being watched, the FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.

For more information click here.

What I did was monitoring the parent folder and sub folders and logged every changes in it. To include sub directories I used the following property.

watcher.IncludeSubdirectories=true;

Some googling suggesting to use timer to detect changes. But I don't know how effective it is.

 
精彩推荐
图片推荐