我发现.Net FileSystemWatcher类对于编写实用程序非常方便,这些实用程序会在文件显示在其监视文件夹中时自动生效。在* nix世界中是否有任何相当于此功能的内容可以让我观看文件夹(可能还有它的所有子目录)?
编辑:最好不要使用内核补丁。
答案 0 :(得分:13)
那将是Gamin the File Alteration Monitor或Inotify。
编辑:Mono确实有Gamin绑定 - 事实上,它的FileSystemWatcher实现使用Gamin。请参阅http://www.mono-project.com/FAQ:_Technical(搜索FileSystemWatcher页面,遗憾的是,FAQ没有锚点。)
答案 1 :(得分:13)
问候, 我想在Ubuntu 10.10中使用Mono中的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
答案 2 :(得分:6)
正如已经说过的,Mono有类“System.IO.FileSystemWatcher”,这是相关的链接: http://www.go-mono.com/docs/monodoc.ashx?link=T%3aSystem.IO.FileSystemWatcher
“Mono的实施 FileSystemWatcher有多个 后端。这是必要的,因为 并非所有支持的操作系统 单声道具有所有必要的功能 提供预期的功能 通过申请。
如果是操作系统内核 支持观看目录(inotify 在Linux上,在BSD或OSX上的KEvents) 功能使用;否则它会下降 回到使用Gamin或FAM 库(这些库提供了一个 用于监控目录的API)和if 这些功能都不可用, Mono将每750毫秒轮询一次 观看的目录。
您可以强制轮询行为 (而不是使用内核支持) 通过设置MONO_MANAGED_WATCHER 执行前的环境变量 你的申请。这可能很有用 对于不支持的文件系统 inotify仍然需要轮询 检测变化。“
答案 3 :(得分:2)
是,dnotify和inotify。
我不知道Mono是否有这些包装,但值得检查。
答案 4 :(得分:2)
如果您正在使用精彩的QT库(www.qtsoftware.com),它将作为QFileSystemWatcher包含在内。