我在码头容器中的程序使用FileSystemWatcher来监视本地文件夹。我挂载目录时:
docker run /c/Users/Support/Desktop/inbox:/Users/Support/Desktop/inbox -v /c/Users/Support/Desktop/outbox:/Users/Support/Desktop/outbox -it -名称为workbeanRun workbean
在容器运行时,我使用Docker查看它。它可以看到收件箱和发件箱目录以及它中的任何文件。但是,当我向收件箱中抛出一个新文件时,FileSystemWatcher事件不会触发。代码没有什么问题,因为如果我不使用码头容器,它就能正常运行。
在安装方向盘时,我还需要做些什么?或者FileSystemWatcher甚至可以在容器内使用?
好吧,按照要求,这是一个程序:
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);
}
}
}
}
发布于 2017-11-15 07:41:00
看来这就是正在发生的事情:
本地驱动器上的收件箱文件夹绑定到容器中的收件箱文件夹。如果将文件添加到一个文件中,它们就会出现在另一个文件中。
但是,FileSystemWatcher只监视容器中的收件箱。如果我将一个文件添加到容器中的收件箱中,那么FileSystemWatcher会触发一个事件。
但是当将文件添加到本地收件箱时,什么都不会发生,因为FileSystemWatcher没有监视这一个文件夹,即使这两个文件夹是相互绑定的。
https://stackoverflow.com/questions/47274362
复制相似问题