微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用FileSystemWatcher监视父目录下特定名称的所有文件夹?

如何解决如何使用FileSystemWatcher监视父目录下特定名称的所有文件夹?

我有一个类似以下的目录结构,其中csv文件可以添加到任何子目录中。 (业务逻辑是,首先将订单文件保存在“供应商”文件夹下,然后检查后将其移至“已处理”文件夹。)

我只想监视名为“已处理”的文件夹。例如,如果有文件添加到“已处理”文件夹,我想得到通知并在回调方法中执行某些操作。如果将文件添加到“供应商”文件夹下,我想忽略它们。我应该如何配置FileSystemWatcher来实现这一目标?

enter image description here

这就是我现在所拥有的。

public static void Watch()
{
   FileSystemWatcher watcher = new FileSystemWatcher();
   watcher.Path = path; //here is the path of the "Order" folder;
   watcher.Created += FileSystemWatcher_Created;
   watcher.EnableRaisingEvents = true;         
}

private static void FileSystemWatcher_Created(object source,FileSystemEventArgs e)
{
   //do something when there are new files added to the watched directory
}

解决方法

FileSystemWatcher具有一个名为IncludeSubdirectories的属性,如果为true,则IncludeSubdirectories会遍历整个子树,而不仅仅是直接子目录。

所以一个例子就是这样(修改了MSDN例子):

private static void Watch(string watch_folder)
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.IncludeSubdirectories = true;
    watcher.Path = watch_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;

    // 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;
}

// Define the event handlers.
private static void OnChanged(object source,FileSystemEventArgs e)
{
    // Specify what is done when a file is changed,created,or deleted.
}

private static void OnRenamed(object source,RenamedEventArgs e)
{
    // Specify what is done when a file is renamed.
}

另请参见

更新

这是一个小型控制台示例,该示例说明如何根据@MickyD注释过滤通知以仅对Processed文件夹做出反应:

class Program
{
    static void Main(string[] args)
    {

        try
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(),"Order");
            Console.WriteLine(path);
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path);
            fileSystemWatcher.IncludeSubdirectories = true;
            fileSystemWatcher.Changed += FileSystemWatcher_Changed;
            fileSystemWatcher.EnableRaisingEvents = true;

            Process.GetCurrentProcess().WaitForExit();
            fileSystemWatcher.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private static void FileSystemWatcher_Changed(object sender,FileSystemEventArgs e)
    {
        if (e.Name.Contains("Processed"))
            Console.WriteLine("Processed folder has changed");
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。