---
title: "How do you monitor a network folder for new files?"  
description: "How do you monitor a network folder for new files?"  
author: "ICSM Computer"  
published: 2025-05-18  
updated: 2025-05-18  
canonical: https://www.mindstick.com/interview/34135/how-do-you-monitor-a-network-folder-for-new-files  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# How do you monitor a network folder for new files?

To **monitor a network folder for new files** in C#, you can use the `FileSystemWatcher` class, which works on both local and network paths (e.g., `\\server\share` or a mapped drive).

### Example: Monitor a Network Folder for New Files

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

class Program
{
    static void Main()
    {
        string networkPath = @"\\server\sharedfolder"; // or mapped drive like "Z:\sharedfolder"

        using FileSystemWatcher watcher = new FileSystemWatcher
        {
            Path = networkPath,
            Filter = "*.*", // watch all file types
            NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime,
            IncludeSubdirectories = false, // true if needed
            EnableRaisingEvents = true
        };

        watcher.Created += (s, e) =>
        {
            Console.WriteLine($"New file detected: {e.FullPath}");
        };

        Console.WriteLine("Watching network folder for new files. Press Enter to exit.");
        Console.ReadLine();
    }
}
```

### Notes

1. Ensure the app has **read permissions** on the network share.
2. The `FileSystemWatcher` depends on **underlying OS file change notifications**, so it generally works on **SMB/CIFS shares**.
3. **Some network file systems** (e.g., NFS or certain cloud-mounted drives) may not support change notifications. In that case, use **polling** (see below).

### Alternative: Polling Fallback (if watcher fails on network)

```cs
var knownFiles = new HashSet<string>(Directory.GetFiles(networkPath));

while (true)
{
    var currentFiles = Directory.GetFiles(networkPath);
    foreach (var file in currentFiles)
    {
        if (!knownFiles.Contains(file))
        {
            Console.WriteLine("New file found: " + file);
            knownFiles.Add(file);
        }
    }

    Thread.Sleep(5000); // check every 5 seconds
}
```

### Best Practices

1. Handle exceptions (e.g., access denied, path not found).
2. Use try/catch inside event handlers — network paths can become unavailable.
3. Log the event timestamp and file name for auditing.

## Answers

### Answer by ICSM Computer

To **monitor a network folder for new files** in C#, you can use the `FileSystemWatcher` class, which works on both local and network paths (e.g., `\\server\share` or a mapped drive).

### Example: Monitor a Network Folder for New Files

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

class Program
{
    static void Main()
    {
        string networkPath = @"\\server\sharedfolder"; // or mapped drive like "Z:\sharedfolder"

        using FileSystemWatcher watcher = new FileSystemWatcher
        {
            Path = networkPath,
            Filter = "*.*", // watch all file types
            NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime,
            IncludeSubdirectories = false, // true if needed
            EnableRaisingEvents = true
        };

        watcher.Created += (s, e) =>
        {
            Console.WriteLine($"New file detected: {e.FullPath}");
        };

        Console.WriteLine("Watching network folder for new files. Press Enter to exit.");
        Console.ReadLine();
    }
}
```

### Notes

1. Ensure the app has **read permissions** on the network share.
2. The `FileSystemWatcher` depends on **underlying OS file change notifications**, so it generally works on **SMB/CIFS shares**.
3. **Some network file systems** (e.g., NFS or certain cloud-mounted drives) may not support change notifications. In that case, use **polling** (see below).

### Alternative: Polling Fallback (if watcher fails on network)

```cs
var knownFiles = new HashSet<string>(Directory.GetFiles(networkPath));

while (true)
{
    var currentFiles = Directory.GetFiles(networkPath);
    foreach (var file in currentFiles)
    {
        if (!knownFiles.Contains(file))
        {
            Console.WriteLine("New file found: " + file);
            knownFiles.Add(file);
        }
    }

    Thread.Sleep(5000); // check every 5 seconds
}
```

### Best Practices

1. Handle exceptions (e.g., access denied, path not found).
2. Use try/catch inside event handlers — network paths can become unavailable.
3. Log the event timestamp and file name for auditing.


---

Original Source: https://www.mindstick.com/interview/34135/how-do-you-monitor-a-network-folder-for-new-files

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
