C# Docker中的FileSystemWatcher不会注意到对本地目录的更改
docker容器中的我的程序使用FileSystemWatcher监视本地文件夹。我使用以下命令装载目录: docker run-v/c/Users/Support/Desktop/inbox:/Users/Support/Desktop/inbox-v/c/Users/Support/Desktop/outbox:/Users/Support/Desktop/outbox-it--name workbean运行workbean 我使用Docker Exec在容器运行时查看它。它可以查看收件箱和发件箱目录以及其中的任何文件。但是,当我将新文件放入收件箱时,FileSystemWatcher事件不会触发。代码没有问题,因为如果我不使用docker容器,它运行良好 在安装directores时,我还需要做什么?或者FileSystemWatcher甚至可以在容器中运行 好的,按照要求,这是程序:C# Docker中的FileSystemWatcher不会注意到对本地目录的更改,c#,docker,filesystemwatcher,C#,Docker,Filesystemwatcher,docker容器中的我的程序使用FileSystemWatcher监视本地文件夹。我使用以下命令装载目录: docker run-v/c/Users/Support/Desktop/inbox:/Users/Support/Desktop/inbox-v/c/Users/Support/Desktop/outbox:/Users/Support/Desktop/outbox-it--name workbean运行workbean 我使用Docker Exec在容器运行时查看它。它可以查看收件箱和
using System;
using System.IO;
namespace workbean
{
class Program
{
FileSystemWatcher watcher = new FileSystemWatcher();
static string sourceDir = "/Users/Support/Desktop/inbox";
static string destDir = "/Users/Support/Desktop/outbox";
static void Main(string[] args)
{
Console.WriteLine("Testing...");
Program p = new Program();
while (true) { }
}
public Program()
{
watcher.Path = sourceDir;
watcher.Filter = "*.*";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
}
static void OnCreated(object source, FileSystemEventArgs e)
{
string[] files = Directory.GetFiles(sourceDir);
foreach (var item in files)
{
string destDir2 = destDir + "/" + Path.GetFileName(item);
File.Move(item, destDir2);
}
}
}
}
看来这就是正在发生的事情: “我的本地驱动器”上的“收件箱”文件夹已绑定到容器中的“收件箱”文件夹。如果将文件添加到其中一个,它们将显示在另一个中 但是,FileSystemWatcher只监视容器中的收件箱。如果我将文件添加到容器中的收件箱,则FileSystemWatcher将触发一个事件
但是,在向本地收件箱添加文件时,不会发生任何事情,因为FileSystemWatcher不会监视该文件,即使这两个文件夹相互关联。如果看不到代码,很难确定您是否正确执行了操作。。