2008-11-27 92 views
8

我發現.Net FileSystemWatcher類非常方便編寫實用程序,當文件顯示在他們的監視文件夾中時它們會自動生效。在* nix世界中是否有與此功能相同的功能,可以讓我觀看文件夾(可能還有其所有子目錄)?Linux世界中是否有與.Net FileSystemWatcher等價的東西?

編輯:最好這將是不需要內核補丁的東西。

回答

2

是的,dnotify和inotify

我不知道莫諾是否有這些包裹,但它是值得檢查。

+0

我在其他地方看過,在單聲道環境中運行時,FileSystemWatcher不能與linux文件系統一起工作。不過謝謝。 – Luke 2008-11-27 17:25:51

6

正如已經被說,單有類「System.IO.FileSystemWatcher」,這是相關鏈接: http://www.go-mono.com/docs/monodoc.ashx?link=T%3aSystem.IO.FileSystemWatcher

「Mono的實施 FileSystemWatcher的有多個 後端這是必要的,因爲 不 單支持的所有操作系統都需要 以提供應用程序預計 的功能特徵。

如果操作系統內核 支持使用 功能的觀看目錄(Linux上的inotify ,BSD或OSX上的KEvents);否則就 回用流浪兒或FAM 庫(這些庫提供了一個 API監控的目錄),如果 沒有這些功能都可用, 單將輪詢每750毫秒 看着目錄。

你可以通過執行 您的應用程序之前設置MONO_MANAGED_WATCHER 環境變量強制投票的行爲 (而不是使用的內核支持)。這可能對於不支持 inotify的,但是仍然需要輪詢來 檢測更改文件系統是有用的 。」

2

如果您使用的是精彩的QT庫(www.qtsoftware.com)它包括作爲QFileSystemWatcher 。

13

問候, 我想和大家分享在Ubuntu 10.10中使用單我FileSystemWatcher的觀察,這是在C#中一個非常簡單的實現FileSystemWatcher對象的

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Text; 
using System.IO; 
using System.Reflection; 

namespace FileSystemWatcherSandbox 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      foreach(DictionaryEntry de in Environment.GetEnvironmentVariables()) 
      { 
       Console.WriteLine("{0} = {1}",de.Key,de.Value); 
      } 
      string basePath = AppDomain.CurrentDomain.BaseDirectory; 
      Console.WriteLine("watching: {0}", basePath); 
      FileSystemWatcher fsw = new FileSystemWatcher(basePath); 
      fsw.Changed += new FileSystemEventHandler(fsw_Changed); 
      fsw.Created += new FileSystemEventHandler(fsw_Created); 
      fsw.Deleted += new FileSystemEventHandler(fsw_Deleted); 
      fsw.Error += new ErrorEventHandler(fsw_Error); 
      fsw.Renamed += new RenamedEventHandler(fsw_Renamed); 
      fsw.EnableRaisingEvents = true; 
      fsw.IncludeSubdirectories = true; 
      while (true) 
      { 
       WaitForChangedResult result = fsw.WaitForChanged(WatcherChangeTypes.All,10000); 
       Console.WriteLine(result.TimedOut ? "Time out" : "hmmm"); 
      } 
     } 

     static void fsw_Renamed(object sender, RenamedEventArgs e) 
     { 
      Console.WriteLine("({0}): {1} | {2}", MethodInfo.GetCurrentMethod().Name, e.ChangeType, e.FullPath); 
     } 

     static void fsw_Error(object sender, ErrorEventArgs e) 
     { 
      Console.WriteLine("({0}): {1}", MethodInfo.GetCurrentMethod().Name, e.GetException().Message); 
     } 

     static void fsw_Deleted(object sender, FileSystemEventArgs e) 
     { 
      Console.WriteLine("({0}): {1} | {2}", MethodInfo.GetCurrentMethod().Name, e.ChangeType, e.FullPath); 
     } 

     static void fsw_Created(object sender, FileSystemEventArgs e) 
     { 
      Console.WriteLine("({0}): {1} | {2}", MethodInfo.GetCurrentMethod().Name, e.ChangeType, e.FullPath); 
     } 

     static void fsw_Changed(object sender, FileSystemEventArgs e) 
     { 
      Console.WriteLine("({0}): {1} | {2}", MethodInfo.GetCurrentMethod().Name, e.ChangeType, e.FullPath); 
     } 
    } 
} 

此代碼已經過測試,可以在Windows XP和Ubuntu 10.10上運行。不過,我想指出的是,在Ubuntu 10.10(可能更早的版本)下,FileSystemWatcher的行爲是唯一的。
如果正在監視的目錄不包含子目錄,則調用帶有IncludeSubdirectories屬性設置爲true的FileSystemWatcher將導致FileSystemWatcher忽略事件。但是,如果目標目錄中有子目錄,則IncludeSubdirectories設置爲true將按預期工作。
如果IncludeSubdirectories設置爲false,將始終有效。在這種情況下,FileSystemWatcher將只能看到目標目錄。
我希望這對於希望跨不同操作系統使用Mono並調用FileSystemWatcher類型的程序員很有用。

chickensandwich

相關問題