FileSystemWatcher Constructors

Definition

Initializes a new instance of the FileSystemWatcher class.

Overloads

FileSystemWatcher()

Initializes a new instance of the FileSystemWatcher class.

FileSystemWatcher(String)

Initializes a new instance of the FileSystemWatcher class, given the specified directory to monitor.

FileSystemWatcher(String, String)

Initializes a new instance of the FileSystemWatcher class, given the specified directory and type of files to monitor.

FileSystemWatcher()

Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs

Initializes a new instance of the FileSystemWatcher class.

C#
public FileSystemWatcher();

Examples

The following example creates a FileSystemWatcher object to watch the directory specified at run time. The FileSystemWatcher object watches for changes in LastWrite and LastAccess times, and for the creation, deletion, or renaming of text files in the directory. If a file is changed, created, or deleted, the path to the file displays to the console. When a file is renamed, the old and new paths display to the console.

This example uses the System.Diagnostics and System.IO namespaces.

C#
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);
            }
        }
    }
}

Remarks

You cannot watch a remote computer that does not have Windows NT or Windows 2000. You cannot watch a remote Windows NT 4.0 computer from a Windows NT 4.0 computer.

The following table shows initial property values for an instance of FileSystemWatcher.

Property Initial Value
NotifyFilter bitwise OR combination of LastWrite, FileName, and DirectoryName
EnableRaisingEvents false
Filter "*.*" (Watch all files.)
IncludeSubdirectories false
InternalBufferSize 8192
Path empty string ("")

Note

The component will not watch the specified directory until the Path is set, and EnableRaisingEvents is true.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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

FileSystemWatcher(String)

Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs

Initializes a new instance of the FileSystemWatcher class, given the specified directory to monitor.

C#
public FileSystemWatcher(string path);

Parameters

path
String

The directory to monitor, in standard or Universal Naming Convention (UNC) notation.

Exceptions

The path parameter is null.

The path parameter is an empty string ("").

-or-

The path specified through the path parameter does not exist.

path is too long.

Remarks

Note

The component will not watch the specified directory until the Path is set, and EnableRaisingEvents is true.

The component can watch files on your personal computer, a network drive, or a remote computer.

You cannot watch a remote computer that does not have Windows NT or Windows 2000. You cannot watch a remote Windows NT 4.0 computer from a Windows NT 4.0 computer. The Filter property is set by default to watch all files.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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

FileSystemWatcher(String, String)

Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs

Initializes a new instance of the FileSystemWatcher class, given the specified directory and type of files to monitor.

C#
public FileSystemWatcher(string path, string filter);

Parameters

path
String

The directory to monitor, in standard or Universal Naming Convention (UNC) notation.

filter
String

The type of files to watch. For example, "*.txt" watches for changes to all text files.

Exceptions

The path parameter is null.

-or-

The filter parameter is null.

The path parameter is an empty string ("").

-or-

The path specified through the path parameter does not exist.

path is too long.

Remarks

Note

The component will not watch the specified directory until the Path is set, and EnableRaisingEvents is true.

The component can watch files on your personal computer, a network drive, or a remote computer.

You cannot watch a remote computer that does not have Windows NT or Windows 2000. You cannot watch a remote Windows NT 4.0 computer from a Windows NT 4.0 computer.

See also

Applies to

.NET 10 and other versions
Product Versions
.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, 10
.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