监视映射的 UNC 时 FileSystemWatcher() 问题

发布于 2024-12-03 20:42:22 字数 588 浏览 1 评论 0原文

我对 FileSystemWatcher() 类有疑问。当我监视硬盘上的本地文件时,它可以完美地工作。当我更改为映射的 UNC 时,它不再触发。 UNC 通过提供用户和密码的 NET USE 命令映射到本地驱动器 (X:),这是在启动时在批处理文件中完成的。任何人都知道为什么这不起作用?我已经检查了路径,所有路径都是正确的,因此问题应该与其他内容有关...

fw = new FileSystemWatcher();
        fw.Path = fileInfoPath;
        fw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        fw.Filter = fileInfoName;
        fw.Changed += new FileSystemEventHandler(FileOnChanged);
        fw.Created += new FileSystemEventHandler(FileOnChanged);

感谢帮助! :)

I have a problem with the FileSystemWatcher() Class. It works flawlessly when I'm monitoring a local file on my harddrive. When I change to a mapped UNC, it does not fire anymore. The UNC is mapped to a local drive (X:), with the NET USE command where the user and password are supplied, this is done in a batch file at startup. Anyone that knows why this doesn't work? I have checked the paths, all of them are correct, so the problem should be related to something else...

fw = new FileSystemWatcher();
        fw.Path = fileInfoPath;
        fw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        fw.Filter = fileInfoName;
        fw.Changed += new FileSystemEventHandler(FileOnChanged);
        fw.Created += new FileSystemEventHandler(FileOnChanged);

Help appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻了热茶 2024-12-10 20:42:22

我对这个问题的解决方案是离开 FileSystemWatcher() 并创建我自己的小观察程序。这很简单,但我唯一想看的是文件何时被重写,然后执行一些操作。

这基本上就是我所做的(删除了 try/catch 和一些其他线程的调用):

System.Threading.Timer tFileWatcher;
private string fileTime1 = "";
private string fileTime2 = "";
//
private void Form1_Load(object sender, EventArgs e)
    {
        tFileWatcher = new System.Threading.Timer(ComputeBoundOp2, 0, 0, 500);
        fileTime1 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
        fileTime2 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
    }

private void ComputeBoundOp2(Object state)
    {
        fileTime2 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();

        if (fileTime1 != fileTime2)
        {
        //Do something
        }
    }

My solution to the problem was to leave the FileSystemWatcher() and create my own little watcher. It's very simple, but the only thing I wanted to watch was when the file was rewritten and then perform some action.

This is basically what I do (removed try/catch and some invoking of other threads):

System.Threading.Timer tFileWatcher;
private string fileTime1 = "";
private string fileTime2 = "";
//
private void Form1_Load(object sender, EventArgs e)
    {
        tFileWatcher = new System.Threading.Timer(ComputeBoundOp2, 0, 0, 500);
        fileTime1 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
        fileTime2 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();
    }

private void ComputeBoundOp2(Object state)
    {
        fileTime2 = File.GetLastWriteTime(fileInfo).ToFileTime().ToString();

        if (fileTime1 != fileTime2)
        {
        //Do something
        }
    }
渔村楼浪 2024-12-10 20:42:22

我发现了一种非常酷的方法,可以在 codeproject 的 Windows 服务中使用 FileSystemWatcher 的凭证来获取 UNC。

请参阅 Adrian Hayes 帖子:http://www. codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials

他的解决方案非常有效。

I found a really cool way to get UNC with credentials working with FileSystemWatcher in a windows service on codeproject.

see Adrian Hayes post: http://www.codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials

His solution works a treat.

懒的傷心 2024-12-10 20:42:22

我无法让任何日期和时间触发器起作用。查看文件详细信息,服务器根本没有更改它们。因此,我使用了文件大小触发器(NotifyFilters.Size),这很有用。我留下了其他通知,以防万一我不使用 UNC。

      fsw.Path = Path.GetDirectoryName(currentFilename);
      fsw.Filter = Path.GetFileName(currentFilename);
      fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
      fsw.Changed += new FileSystemEventHandler(fsw_Changed);
      fsw.EnableRaisingEvents = true;  

I couldn't get any of the date and time triggers to work. Looking at the file details the server wasn't changing them at all. So instead I used a trigger on the file size (NotifyFilters.Size) which worked a charm. I left the other notifies in just in case I'm not using a UNC.

      fsw.Path = Path.GetDirectoryName(currentFilename);
      fsw.Filter = Path.GetFileName(currentFilename);
      fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;
      fsw.Changed += new FileSystemEventHandler(fsw_Changed);
      fsw.EnableRaisingEvents = true;  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文