监视映射的UNC时出现FileSystemWatcher()问题 [英] FileSystemWatcher() problem when monitoring mapped UNC

查看:128
本文介绍了监视映射的UNC时出现FileSystemWatcher()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

帮助表示赞赏! :)

推荐答案

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

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.

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

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
        }
    }

这篇关于监视映射的UNC时出现FileSystemWatcher()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆