Home >Backend Development >C++ >How Can I Monitor File Changes in C# Using FileSystemWatcher?

How Can I Monitor File Changes in C# Using FileSystemWatcher?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-13 09:15:47440browse

How Can I Monitor File Changes in C# Using FileSystemWatcher?

Monitor file changes using C#’s FileSystemWatcher

In a C# application, you may need to detect file modifications on disk. This can be easily achieved through the FileSystemWatcher class provided by the .NET Framework.

Use FileSystemWatcher to implement file change detection

  1. Create a FileSystemWatcher instance: Initialize an instance using the FileSystemWatcher constructor. Set its Path attribute to the directory or file to be monitored.
  2. Configure monitoring filters: Use the NotifyFilter property to specify the type of changes to detect. Common options include LastAccess, LastWrite, FileName, and DirectoryName.
  3. Filter file types (optional): Limit monitoring operations to specific file types by setting the Filter property. For example, to monitor only text files, set Filter to "*.txt".
  4. Add event handler: Register an event handler to receive notifications when file changes occur. Common events include Changed, Created, Deleted and Renamed.
<code class="language-csharp">private static void OnChanged(object source, FileSystemEventArgs e)
{
    // 当文件更改、创建或删除时,实现所需的动作。
    Console.WriteLine($"文件: {e.FullPath} {e.ChangeType}");
}

private static void OnRenamed(object source, RenamedEventArgs e)
{
    // 定义在文件重命名时要执行的动作。
    Console.WriteLine($"文件: {e.OldFullPath} 重命名为 {e.FullPath}");
}</code>
  1. Start monitoring: Set the EnableRaisingEvents property to true to start monitoring. When this feature is enabled, any changes in the specified folder or file will trigger the corresponding event handler.

Sample code:

The following code snippet demonstrates how to use FileSystemWatcher to monitor text files in a specific directory:

<code class="language-csharp">public void CreateFileWatcher(string path)
{
    // 创建FileSystemWatcher实例。
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = path;
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.txt";

    // 附加事件处理程序以响应文件更改。
    watcher.Changed += OnChanged;
    watcher.Created += OnChanged;
    watcher.Deleted += OnChanged;
    watcher.Renamed += OnRenamed;

    // 开始监控。
    watcher.EnableRaisingEvents = true;
}</code>

By implementing this mechanism, C# applications can effectively track and respond to file modifications on disk, enabling real-time data synchronization, version control, and other critical operations.

The above is the detailed content of How Can I Monitor File Changes in C# Using FileSystemWatcher?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn