

Your last configuration step is to wire up the method you want to be executed when a change happens that meets your Path, Filter and NotifyFilter conditions.
#Windows monitor folder for new files code#
You'll need to put code like this somewhere in your application to turn that on (this is the code that might go in a different method than the rest of the configuration code and cause you to declare the FileSystemWatcher as a field): The FileSystemWatcher does not, however, actually start watching until you set its EnableRaisingEvents property to true. If you don't set the NotifyFilter property, you'll get the default setting: You'll be notified of changes to the file itself and changes to either the file name or the folder's name. This example not only tracks changes to the file's contents, but also tracks changes to the file's name by Oring together the LastWrite filter and the FileName filter (which catches changes to the file's name):įsw.NotifyFilter = NotifyFilters.LastWrite Or To catch multiple events you can Or multiple filters together. To configure the watcher so that you'll be notified whenever a file is changed, use the NotifyFilters.LastWrite filter, like this:įsw.NotifyFilter = NotifyFilters.LastWrite For example, to be notified when a file's size changes you would set the NotifyFilter to NotifyFilters.Size. In addition to specifying the file or folder you're interested in, you can also specify what changes you want to be notified about by setting the watcher's NotifyFilter property. If you want to monitor a folder's subdirectories, set the watcher's IncludeSubdirectories property to True (the default is False). If you want to be notified about changes to exactly one file, set the filter to a complete file name, without wildcards. This example will catch changes to any file whose extension begins with "nu" in the C:\NuGet folder: If you want to monitor all the files in a folder (or are only interested in changes to the folder) you can skip setting the Filter property.

The folder you want to monitor (which must exist) using the watcher's Path property.This consists of specifying up to two things: Somewhere in your application (probably in your class's constructor), you'll need to configure the watcher. However, if you do all of the object's configuration in a single method you can declare the variable inside the method with its configuration code. If you're going to access the FileSystemWatcher from multiple places in your application, you should declare it as a field (that is, in your class, but outside any method or property). The first step in configuring the watcher is to delcare a variable to hold an instance of the FileSystemWatcher class. There are three parts to using the FileSystemWatcher: You have to initialize it (tell the FileSystemWatcher what you're interested in), turn it on and provide a method that will execute when something you're interested in happens.
#Windows monitor folder for new files windows#
As a result, this tool works best in a desktop application or a Windows service. You can't, unfortunately, use the FileSystemWatcher to determine if a folder or file has changed since the last time your program was running. One warning: To catch changes to the file, you have to keep monitoring the file or folder you're interested in. Once you know something's happened, you can take action. NET Framework FileSystemWatcher to notify you when something's happened to the contents of a folder or to a specific file. Rather than constantly polling a folder to see what's different, use the Microsoft. Sometimes you're keenly interested in knowing when a file or folder changes.
