无法捕获网络文件更改

时间:2013-07-24 10:33:26

标签: c# .net

我使用下面的c#代码来捕获文件系统中的所有chnge(比如文件重命名或创建等),并且效果很好。但是当我将路径从“D:\”更改为“\\ ServerName \ folder”时 - 程序停止工作。但在MSDN FileSystemWatcher类描述中可悲:“...您可以创建一个组件来监视本地计算机上的文件,网络驱动器远程计算机 ... “ 你可以帮我强制我的代码使用网络文件夹PLZ。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace consoleWatcher
{
    class Program
    {
        static void Main(string[] args)
        {
            FileSystemWatcher myWatcher = new FileSystemWatcher("D:\\");
            myWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite  | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            myWatcher.Changed += new FileSystemEventHandler(OnChanged);
            myWatcher.Created += new FileSystemEventHandler(OnChanged);
            myWatcher.Deleted += new FileSystemEventHandler(OnChanged);
            myWatcher.Renamed += new RenamedEventHandler(OnRenamed);

            myWatcher.IncludeSubdirectories = true;
            myWatcher.EnableRaisingEvents = true;

            Console.Read();

        }

        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.

            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.

            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }

    }
}

1 个答案:

答案 0 :(得分:-1)