微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FileSystemWatcher for FTP 不通知新文件添加到文件夹

如何解决FileSystemWatcher for FTP 不通知新文件添加到文件夹

我在 Windows 控制台应用程序中运行我的程序,但我已经在网络上映射了一个 NFS 驱动器,我在 Linux 机器上通过 SFTP 下载文件。当我的 shellscript 将文件带回该文件夹时,我的应用程序不会在文件到达时捕获,但是如果我从 Windows 或 Linux 复制文件,它可以正常工作。当 FileSystemWatcher 来自 SFTP 脚本时,它几乎就像不起作用一样。

这里是代码示例

Global.MainPath = Console.ReadLine();
p.path = Global.MainPath;
MonitorDirectory(p.path);
Console.WriteLine("Checking folder " + p.path);
Console.WriteLine(" ");

while (true)
{
    MonitorDirectory(p.path);
    ShowContent(p.path);
    Console.WriteLine(" ");
    Console.Write("Enter q to quit: ");
    line = Console.ReadLine().ToLower();
    if (line == "q" || line == "quit")
    {
        break;
    }
    else
    {
        Console.Clear();
    }
}

// and here's the MonitorDirectory & FileSystemWatcher_Created methods

private static void MonitorDirectory(string path)
{
    FileSystemWatcher fileSystemWatcher = new FileSystemWatcher
    { Path = path };
    fileSystemWatcher.Created += FileSystemWatcher_Created;
    fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;
    fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
    fileSystemWatcher.Changed += FileSystemWatcher_Changed;
    fileSystemWatcher.EnableRaisingEvents = true;
 }

private static void FileSystemWatcher_Created(object sender,FileSystemEventArgs e)
{
    Mypath p = new Mypath();
    Console.Clear();
    p.path = Global.MainPath;
    string filePath = p.path + "\\" + e.Name;
    Console.WriteLine("File created: {0}",e.Name);
    SendEmailtoContacts("File Created " + e.Name,filePath + " has been created from the folder");
    ShowContent(p.path);
}

解决方法

您在每个循环中都重新初始化了系统文件观察器。 删除这个:

MonitorDirectory(p.path);

来自循环。

顺便说一句:我不会使用静态方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。