首页 > 解决方案 > C# Filesystemwatcher 没有带有网络路径的事件

问题描述

我有以下问题:我制作了一个小型 C# 程序,它使用 FileSystemWatcher 来监视文件夹并在文件更改时执行请求。使用本地路径,一切正常。但是,如果我现在将路径更改为网络路径,则不会再触发任何事件。

我使用网络路径作为 UNC 路径并将路径映射到驱动器号。两者结果相同。FilesSystemWatcher 没有给出错误,但也没有触发事件。

我使用以下代码来初始化观察者

        static void Main(string[] args)
        {
            try
            {
                var filewatcher = new FileSystemWatcher(@"\\FILESERVER\Test");
                filewatcher.NotifyFilter = NotifyFilters.FileName
                     | NotifyFilters.LastWrite;


                filewatcher.Changed += OnChanged;
                filewatcher.Error += OnError;

                filewatcher.Filter = " *.*";
                filewatcher.IncludeSubdirectories = true;
                filewatcher.EnableRaisingEvents = true;
                filewatcher.InternalBufferSize = 64*1024;

                Console.WriteLine("Press enter to exit.");
                Console.ReadLine();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine($"Changed:");
            if (isFileLocked(new FileInfo(e.FullPath)) )
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}, {e.ChangeType}");
            //CreateOrUpdate(e.FullPath);
        }

        private static void OnError(object sender, ErrorEventArgs e) =>
            PrintException(e.GetException());

        private static void PrintException(Exception ex)
        {
            if (ex != null)
            {
                Console.WriteLine($"Message: {ex.Message}");
                Console.WriteLine("Stacktrace:");
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine();
                PrintException(ex.InnerException);
            }
        }

标签: c#filesystemwatcher

解决方案


推荐阅读