FileSystemWatcher不会通知有关Samba共享的事件

问题描述

我在C#应用程序中使用FileSystemWatcher,并且该应用程序在服务器上作为Windows服务运行。观察者路径是来自另一个网络的Samba共享。 当监视程序的文件夹路径是服务器上的文件夹时,该应用程序完全正常运行,但是使用共享文件夹时不会引发任何事件-在该文件夹中创建文件时需要通知我,然后将其移至另一个重命名,阅读等等。 (我也尝试过watcher.Changed事件,但那里也没有发生任何事情)

在这里发现了一个类似的问题: FileSystemWatcher with Samba on Linux

有人知道FSW是否仍然对Samba共享文件夹有问题吗? 我已经尝试使用StreamReader和StreamWriter来测试我是否甚至可以访问共享文件夹-这可以正常工作。我还考虑过如果FSW“中断”(如上面的问题中提到的)将EnableRaisingEvents重置为true,但是我有点困惑如何甚至发现它是否中断-因为我没有收到错误,所以根本什么也没做。

这是我的watcher类的一部分,它作为BackgroundService运行:

protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
    Initialize();
    TestAccessability();
    RunFileWatcher();
    return Task.CompletedTask;
}

private void TestAccessability()
{
    // Get the directories currently on the shared drive.
    DirectoryInfo[] sDirs = new DirectoryInfo(@"\\10.18.249.8\halit4ind$").GetDirectories();

    // Write each directory name to a file.
    using (StreamWriter sw = new StreamWriter(importPath + "\\SDriveDirs.txt"))
    {
        foreach (DirectoryInfo dir in sDirs)
        {
            sw.WriteLine(dir.Name);
        }
    }

    // Read and show each line from the file.
    string line = "";
    using (StreamReader sr = new StreamReader(importPath + "\\SDriveDirs.txt"))
    {
        while ((line = sr.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }
}

private void RunFileWatcher()
{
    logger.Loginformation($"RunFileWatcher watching path {importPath}");
    watcher = new FileSystemWatcher
    {
        Path = @importPath,Filter = "*.csv"
    };

    watcher.Created += OnCreated;
    watcher.Changed += OnCreated;
    watcher.EnableRaisingEvents = true;
}

private void OnCreated(object source,FileSystemEventArgs e)
{
    logger.Loginformation($"File {e.FullPath} created/changed - Type: {e.ChangeType}");
    if (e.ChangeType == WatcherChangeTypes.Created)
    {
        var newFilename = TryMoveFiletoWork(e.Name);
        MoveFiletoArchiv(newFilename);
    }
}

这是我在控制台中获得的输出(我尝试在\ 10.18.249.8 \ halit4ind $ \ OutBox创建文件,但什么也没发生):

pers
InBox
OutBox
Work
Archiv
[07:19:57 INF] RunFileWatcher watching path \\10.18.249.8\halit4ind$\OutBox
[07:19:57 DBG] Failed to locate the development https certificate at 'null'.
[07:19:57 DBG] Hosting started
[07:19:57 DBG] Loaded hosting startup assembly InfoniqaServiceHali
Hosting environment: Production
Content root path: D:\Services\InfoniqaServiceHali
Now listening on: http://0.0.0.0:7040
Application started. Press Ctrl+C to shut down.

这是我使用其他路径时得到的输出

pers
InBox
OutBox
Work
Archiv
[08:49:35 INF] RunFileWatcher watching path D:\temp\OutBox
[08:49:35 DBG] Failed to locate the development https certificate at 'null'.
[08:49:35 DBG] Hosting started
[08:49:35 DBG] Loaded hosting startup assembly InfoniqaServiceHali
Hosting environment: Production
Content root path: D:\Services\InfoniqaServiceHali
Now listening on: http://0.0.0.0:7040
Application started. Press Ctrl+C to shut down.
[08:49:44 INF] File D:\temp\OutBox\Personal.csv created/changed - Type: Created
[08:49:44 INF] TryMoveFiletoWork Personal.csv

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)