2009-04-17 88 views
28

当在Windows系统上的某个目录中进行更改时,需要立即通知该程序发生更改。如何监视Windows目录的更改?

发生更改时是否有某种方式执行程序?

我不是C/C++/.NET程序员,所以如果我可以设置一些东西,以便更改可以触发批处理文件,那么这将是理想的。

+0

它可以通过VBScript和WMI完成,请参阅此实现http://www.go-geek.com/tips-n-tricks/monitor-directory-for-new-files-with-wsh.html – Pradeep 2012-08-07 13:57:31

回答

18

使用类似下面的FileSystemWatcher来创建一个WatcherCreated事件()。我使用它来创建一个Windows服务,该服务监视网络文件夹,然后在新文件到达时通过电子邮件发送指定的组。

// Declare a new FILESYSTEMWATCHER 
    protected FileSystemWatcher watcher; 
    string pathToFolder = @"YourDesired Path Here"; 

    // Initialize the New FILESYSTEMWATCHER 
    watcher = new FileSystemWatcher {Path = pathToFolder, IncludeSubdirectories = true, Filter = "*.*"}; 
    watcher.EnableRaisingEvents = true; 
    watcher.Created += new FileSystemEventHandler(WatcherCreated); 

    void WatcherCreated(object source , FileSystemEventArgs e) 
    { 
     //Code goes here for when a new file is detected 
    } 
3
+0

我需要Visual Studio?我可以在VBScript中使用它吗? – Liam 2009-04-17 15:43:52

+0

FileSystemWatcher是.Net所以是的这个解决方案将需要Visual Studio。 – 2009-04-17 15:47:18

6

如果你想要的东西非程序尝试[email protected] ...但在这种情况下,问题将不属于这里!

+0

的确,这个问题可能更适合超级用户甚至服务器故障,但是这个答案直接回答了OP的问题:“有什么方法在发生变化时执行程序?”。 – Pat 2012-07-17 17:04:37

+0

GiPo @ FileUtilities似乎已被删除,但如果您需要非程序化的东西,http://www.myassays.com/folder-poll会做类似的事情。 – 2016-09-05 10:23:42

1

没有实用程序或与Windows自带的程序来做到这一点。需要一些编程。

正如在另一个答案中指出的,.NET的FileSystemWatcher是最简单的方法。

本地API ReadDirectoryChangesW比较难使用(需要了解完成端口)。

7

FileSystemWatcher是正确的答案,只不过它曾经是FileSystemWatcher一次仅适用于“少数”更改。这是因为操作系统缓冲区。在实践中,每当复制很多小文件时,保存文件名称变化的缓冲区就会被超载。这个缓冲区并不是真正的跟踪最近变化的正确方法,因为当缓冲区已满时操作系统将不得不停止写入以防止超限。

Microsoft改为提供其他功能(编辑:如更改日志)以真正捕获所有更改。这基本上是备份系统使用的设施,并且对记录的事件很复杂。并且也很少记录。

一个简单的测试是生成大量的小文件,并查看是否全部由FileSystemWatcher报告。如果您遇到问题,我建议避开整个问题并按定时间隔扫描文件系统以进行更改。

2

我在寻找监控文件系统活动的方式时出现在这个页面上。我拿了折射圣骑士的职位和他分享,写了一个快速和肮脏的工作C#实现FileSystemWatcher

using System; 
using System.IO; 

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

      //Based on http://stackoverflow.com/questions/760904/how-can-i-monitor-a-windows-directory-for-changes/27512511#27512511 
      //and http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx 

      string pathToFolder = string.Empty; 
      string filterPath = string.Empty; 
      const string USAGE = "USAGE: Folderwatch.exe PATH FILTER \n\n e.g.:\n\n Folderwatch.exe c:\\windows *.dll"; 

      try 
      { 
       pathToFolder = args[0]; 

      } 
      catch (Exception) 
      { 
       Console.WriteLine("Invalid path!"); 
       Console.WriteLine(USAGE); 
       return; 
      } 

      try 
      { 
       filterPath = args[1]; 
      } 
      catch (Exception) 
      { 
       Console.WriteLine("Invalid filter!"); 
       Console.WriteLine(USAGE); 
       return; 

      } 

      FileSystemWatcher watcher = new FileSystemWatcher(); 

      watcher.Path = pathToFolder; 
      watcher.Filter = filterPath; 

      watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | 
       NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | 
       NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; 

      // 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("Monitoring File System Activity on {0}.", pathToFolder); 
      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); 
     } 
    } 
} 

要使用此功能,下载的Visual Studio(Express将做)。创建一个名为Folderwatch的新C#控制台应用程序,并将我的代码复制并粘贴到您的Program.cs中。

作为替代方案,您可以使用Sys Internals Process Monitor:Process Monitor它可以监视文件系统和更多。