---
title: "How do you monitor file changes in a directory using FileSystemWatcher?"  
description: "How do you monitor file changes in a directory using FileSystemWatcher?"  
author: "ICSM Computer"  
published: 2025-05-06  
updated: 2025-05-21  
canonical: https://www.mindstick.com/forum/161582/how-do-you-monitor-file-changes-in-a-directory-using-filesystemwatcher  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# How do you monitor file changes in a directory using FileSystemWatcher?

How do you monitor [file changes](https://www.mindstick.com/interview/34100/how-can-you-watch-for-file-changes-like-creation-or-deletion-in-real-time) in a [directory](https://www.mindstick.com/forum/226/how-to-get-application-directory-using-c-sharp-csharp) using `FileSystemWatcher`?

## Replies

### Reply by Utpal Vishwas

To **monitor [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) changes in a directory** in C#, use the built-in `FileSystemWatcher` class from the `System.IO` namespace.

## What FileSystemWatcher Can Track

1. It can detect changes such as:
2. File creation
3. File modification
4. File deletion
5. File renaming

## Basic Example: Monitor a Directory

```cs
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string pathToWatch = @"C:\MyWatchedFolder";

        using (FileSystemWatcher watcher = new FileSystemWatcher())
        {
            watcher.Path = pathToWatch;

            // Watch for changes in LastWrite and LastAccess time, and file name or directory name changes.
            watcher.NotifyFilter = NotifyFilters.LastWrite
                                 | NotifyFilters.FileName
                                 | NotifyFilters.DirectoryName;

            // Filter: watch all files
            watcher.Filter = "*.*";

            // Event handlers
            watcher.Changed += OnChanged;
            watcher.Created += OnChanged;
            watcher.Deleted += OnChanged;
            watcher.Renamed += OnRenamed;

            // Begin watching
            watcher.EnableRaisingEvents = true;

            Console.WriteLine($"Monitoring changes in: {pathToWatch}");
            Console.WriteLine("Press [Enter] to exit...");
            Console.ReadLine();
        }
    }

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        Console.WriteLine($"File {e.ChangeType}: {e.FullPath}");
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        Console.WriteLine($"File Renamed: {e.OldFullPath} => {e.FullPath}");
    }
}
```

## Notes

1. `EnableRaisingEvents = true` must be set to start monitoring.
2. `NotifyFilter` helps filter what kind of changes you want to track.
3. `Filter = "*.txt"` will limit monitoring to specific file types.
4. You can also watch **subdirectories** by setting `IncludeSubdirectories = true`.

## Optional: Watch Subdirectories

```plaintext
watcher.IncludeSubdirectories = true;
```

## Considerations

- FileSystemWatcher may miss rapid successive changes (e.g., thousands of changes quickly).
- You might want to debounce events using a timer or queue for performance in heavy-use cases.
- On Linux/macOS (via .NET Core), it uses platform-specific mechanisms like inotify or FSEvents.


---

Original Source: https://www.mindstick.com/forum/161582/how-do-you-monitor-file-changes-in-a-directory-using-filesystemwatcher

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
