FileSystemWatcher 不响应虚拟机及其主机共享的文件夹中的文件事件
FileSystemWatcher not responding to file events in folder shared by virtual machine and its host
我在 Ubuntu VM 上有一个 dnx 控制台应用程序,它监视与主机共享的文件夹 OS (Windows 8.1)。当 Ubuntu VM 上的共享文件夹中发生文件更改时,控制台应用程序会响应它们,但不会在主机上进行文件更改时响应。怎么来的,有没有办法让它起作用?
using System;
using System.IO;
public class Program
{
public static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "/media/sf_shared";
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Changed += new FileSystemEventHandler(Respond);
watcher.Created += new FileSystemEventHandler(Respond);
watcher.Deleted += new FileSystemEventHandler(Respond);
watcher.EnableRaisingEvents = true;
Console.ReadKey();
}
private static void Respond(object source, FileSystemEventArgs e)
{
Console.WriteLine("Hej");
}
}
在 Linux,Mono 将使用 inotify 作为其首选事件发布后端到 FileSystemWatcher class。您遇到的问题是 Windows 对文件系统的更改不会导致 Ubuntu/Linux 文件系统发布事件... Windows 到 Windows UNC 共享路径确实会发布文件系统事件,但跨平台文件系统共享 'typically' 不会……即在 VM 主机中嵌入 Samba……不是硬性规定,但每个部署的环境都需要测试。
在你的情况下发生这种情况的唯一方法是轮询系统,mono 确实支持,如果它支持 5 个不同的后端文件系统事件开箱即用。
您可以强制它使用内置的轮询方法(在创建文件监视对象之前执行此操作):
Environment.SetEnvironmentVariable ("MONO_MANAGED_WATCHER", "1");
或在 运行 应用程序之前设置环境变量:
export MONO_MANAGED_WATCHER=1
买家注意:这是轮询时的性能问题。这将在 750 毫秒内对您在观察者中定义的内容进行目录扫描。将您的观察者保留在一个目录中,没有子目录,并且最好将其过滤到一个非常小的文件子集(一个?),并且如果可能的话,该目录中只有一个文件....
这曾经在单声道文档中记录过,但我再也找不到它了(但它在源代码中;-):
在线文档:
http://docs.go-mono.com/monodoc.ashx?link=T%3aSystem.IO.FileSystemWatcher
旧文档注释(可能在单声道的手册页中?):
"Mono's implementation of the FileSystemWatcher has multiple backends.
This is necessary because not all operating systems supported by Mono
have all the features necessary to provide the functionality expected
by applications.
If the operating system kernel supports watching directories (inotify
on Linux, KEvents on BSD or OSX) that feature is used; Otherwise it
falls back to using the Gamin or FAM libraries (these libraries
provide an API to monitor directories) and if none of those features
are available, Mono will poll every 750 milliseconds the directories
watched.
You can force the polling behavior (instead of using the kernel
support) by setting the MONO_MANAGED_WATCHER environment variable
before executing your application. This might be useful for
filesystems that do not support inotify and still require polling to
detect changes."
我在 Ubuntu VM 上有一个 dnx 控制台应用程序,它监视与主机共享的文件夹 OS (Windows 8.1)。当 Ubuntu VM 上的共享文件夹中发生文件更改时,控制台应用程序会响应它们,但不会在主机上进行文件更改时响应。怎么来的,有没有办法让它起作用?
using System;
using System.IO;
public class Program
{
public static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "/media/sf_shared";
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Changed += new FileSystemEventHandler(Respond);
watcher.Created += new FileSystemEventHandler(Respond);
watcher.Deleted += new FileSystemEventHandler(Respond);
watcher.EnableRaisingEvents = true;
Console.ReadKey();
}
private static void Respond(object source, FileSystemEventArgs e)
{
Console.WriteLine("Hej");
}
}
在 Linux,Mono 将使用 inotify 作为其首选事件发布后端到 FileSystemWatcher class。您遇到的问题是 Windows 对文件系统的更改不会导致 Ubuntu/Linux 文件系统发布事件... Windows 到 Windows UNC 共享路径确实会发布文件系统事件,但跨平台文件系统共享 'typically' 不会……即在 VM 主机中嵌入 Samba……不是硬性规定,但每个部署的环境都需要测试。
在你的情况下发生这种情况的唯一方法是轮询系统,mono 确实支持,如果它支持 5 个不同的后端文件系统事件开箱即用。
您可以强制它使用内置的轮询方法(在创建文件监视对象之前执行此操作):
Environment.SetEnvironmentVariable ("MONO_MANAGED_WATCHER", "1");
或在 运行 应用程序之前设置环境变量:
export MONO_MANAGED_WATCHER=1
买家注意:这是轮询时的性能问题。这将在 750 毫秒内对您在观察者中定义的内容进行目录扫描。将您的观察者保留在一个目录中,没有子目录,并且最好将其过滤到一个非常小的文件子集(一个?),并且如果可能的话,该目录中只有一个文件....
这曾经在单声道文档中记录过,但我再也找不到它了(但它在源代码中;-):
在线文档: http://docs.go-mono.com/monodoc.ashx?link=T%3aSystem.IO.FileSystemWatcher
旧文档注释(可能在单声道的手册页中?):
"Mono's implementation of the FileSystemWatcher has multiple backends. This is necessary because not all operating systems supported by Mono have all the features necessary to provide the functionality expected by applications.
If the operating system kernel supports watching directories (inotify on Linux, KEvents on BSD or OSX) that feature is used; Otherwise it falls back to using the Gamin or FAM libraries (these libraries provide an API to monitor directories) and if none of those features are available, Mono will poll every 750 milliseconds the directories watched.
You can force the polling behavior (instead of using the kernel support) by setting the MONO_MANAGED_WATCHER environment variable before executing your application. This might be useful for filesystems that do not support inotify and still require polling to detect changes."