FileSystemWatcher使用方法具体解释

一、FileSystemWatcher简介

.NET Framework中FileSystemWatcher是一个用于监视文件或目录的变化的组件。组件能够监控文件或目录的创建、重命名、修改或删除等事件,并且能够在发生这些事件时自动通知应用程序。

二、FileSystemWatcher基本属性

1. Path:被监视的文件或目录的路径。

2. Filter:指定要监视的文件或类型的通配符。

3. NotifyFilter:指定要监视的文件或目录变化的类型,如文件名、最后修改时间、文件大小、子目录等。

4. IncludeSubdirectories:指定是否监视子目录。

5. EnableRaisingEvents:指定是否启动监视。

6. InternalBufferSize:指定要缓冲的事件的字节数。

三、FileSystemWatcher基本事件

1. Changed:文件或者目录被修改时触发。

2. Created:文件或者目录被创建时触发。

3. Deleted:文件或者目录被删除时触发。

4. Renamed:文件或者目录被重命名时触发。

四、FileSystemWatcher使用方法

1. 声明一个FileSystemWatcher对象

```

using System.IO;

FileSystemWatcher watcher = new FileSystemWatcher();

```

2. 设置FileSystemWatcher的属性

```

watcher.Path = @"C:\test";

watcher.Filter = "*.txt";

watcher.NotifyFilter = NotifyFilters.LastWrite;

watcher.IncludeSubdirectories = true;

watcher.InternalBufferSize = 65536;

watcher.EnableRaisingEvents = true;

```

3. 编写相应事件的处理方法

```

private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)

{

Console.WriteLine("Changed: {0}", e.FullPath);

}

private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)

{

Console.WriteLine("Created: {0}", e.FullPath);

}

private void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)

{

Console.WriteLine("Deleted: {0}", e.FullPath);

}

private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)

{

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

}

```

4. 将相应事件的处理方法绑定至对应的FileSystemWatcher事件上

```

watcher.Changed += new FileSystemEventHandler(FileSystemWatcher_Changed);

watcher.Created += new FileSystemEventHandler(FileSystemWatcher_Created);

watcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_Deleted);

watcher.Renamed += new RenamedEventHandler(FileSystemWatcher_Renamed);

```

5. 如需停止FileSystemWatcher的监视,可使用以下代码

```

watcher.EnableRaisingEvents = false;

watcher.Dispose();

```

五、案例说明

假设现在有一个文件夹,其中存放着一些扩展名为.txt的文件。当该文件夹中有文件被创建、修改、重命名或删除时,我们希望能够及时得到通知。

完整代码如下:

```

using System;

using System.IO;

class Program

{

static void Main(string[] args)

{

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"C:\test";

watcher.Filter = "*.txt";

watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

watcher.IncludeSubdirectories = true;

watcher.InternalBufferSize = 65536;

watcher.EnableRaisingEvents = true;

watcher.Changed += new FileSystemEventHandler(FileSystemWatcher_Changed);

watcher.Created += new FileSystemEventHandler(FileSystemWatcher_Created);

watcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_Deleted);

watcher.Renamed += new RenamedEventHandler(FileSystemWatcher_Renamed);

Console.WriteLine("Press any key to exit.");

Console.ReadLine();

watcher.EnableRaisingEvents = false;

watcher.Dispose();

}

private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)

{

Console.WriteLine("Changed: {0}", e.FullPath);

}

private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)

{

Console.WriteLine("Created: {0}", e.FullPath);

}

private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)

{

Console.WriteLine("Deleted: {0}", e.FullPath);

}

private static void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)

{

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

}

}

```

在本例中,当我们将一个新的.txt文件夹放入C:\test中时,将会在控制台输出Created: C:\test\新建文本文档.txt。当该文件被修改、重命名或删除时,也会相应地输出相应的信息。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.37seo.cn/

点赞(57) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部