使用英语阅读

通过


FileSystemWatcher.Path 属性

定义

获取或设置要监视的目录的路径。

public string Path { get; set; }
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.IO.IODescription("FSW_Path")]
public string Path { get; set; }
[System.IO.IODescription("FSW_Path")]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Path { get; set; }
[System.IO.IODescription("FSW_Path")]
[System.ComponentModel.SettingsBindable(true)]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Path { get; set; }
[System.ComponentModel.SettingsBindable(true)]
public string Path { get; set; }

属性值

要监视的路径。 默认值为空字符串("")。

属性

例外

指定的路径不存在或找不到。

- 或 -

指定的路径包含通配符。

- 或 -

指定的路径包含无效路径字符。

示例

以下示例创建 ,FileSystemWatcher以watch运行时指定的目录。 对于目录中文本文件的LastWriteLastAccess创建、删除或重命名,组件设置为watch和时间的更改。 如果文件已更改、创建或删除,该文件的路径将输出到控制台。 重命名文件时,新旧路径将打印到控制台。

using System;
using System.IO;

namespace MyNamespace
{
    class MyClassCS
    {
        static void Main()
        {
            using var watcher = new FileSystemWatcher(@"C:\path\to\folder");

            watcher.NotifyFilter = NotifyFilters.Attributes
                                 | NotifyFilters.CreationTime
                                 | NotifyFilters.DirectoryName
                                 | NotifyFilters.FileName
                                 | NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.Security
                                 | NotifyFilters.Size;

            watcher.Changed += OnChanged;
            watcher.Created += OnCreated;
            watcher.Deleted += OnDeleted;
            watcher.Renamed += OnRenamed;
            watcher.Error += OnError;

            watcher.Filter = "*.txt";
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

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

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}");
        }

        private static void OnCreated(object sender, FileSystemEventArgs e)
        {
            string value = $"Created: {e.FullPath}";
            Console.WriteLine(value);
        }

        private static void OnDeleted(object sender, FileSystemEventArgs e) =>
            Console.WriteLine($"Deleted: {e.FullPath}");

        private static void OnRenamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine($"Renamed:");
            Console.WriteLine($"    Old: {e.OldFullPath}");
            Console.WriteLine($"    New: {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);
            }
        }
    }
}

注解

这是目录的完全限定路径。 IncludeSubdirectories如果 属性为 true,则此目录是系统监视更改的根目录;否则,它是唯一监视的目录。 若要watch特定文件,请将 Path 属性设置为完全限定的正确目录,并将 Filter 属性设置为文件名。

属性 Path 支持通用命名约定 (UNC) 路径。

备注

必须先设置此属性,然后组件才能watch更改。

重命名目录时, FileSystemWatcher 会自动重新附加到新重命名的项。 例如,如果将 属性设置为 Path “C:\My Documents”,然后手动将目录重命名为“C:\Your Documents”,则组件将继续侦听新重命名的目录上的更改通知。 但是,当你请求 Path 属性时,它包含旧路径。 发生这种情况是因为组件根据句柄(而不是目录名称)确定要监视的目录。 重命名不会影响句柄。 因此,如果销毁组件,然后重新创建它而不更新 Path 属性,应用程序将失败,因为目录不再存在。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅