.NET Tip: Receive Notification When a File is Updated Paradise Valley AZ

Do you need to know when a file or directory has been updated? The FileSystemWatcher class gives you a very easy means to monitor the file system.

Local Companies

Software Services Group
480-483-8904
8687 E Via De Ventura
Scottsdale, AZ
Mac Media Inc
480-964-6352
6928 E 5th Ave
Scottsdale, AZ
Avolve Software
602-482-8334
4835 E Cactus RD
Scottsdale, AZ
Jda Software
480-308-3000
14400 N 87th St
Scottsdale, AZ
Phoenix Phive Software Corporation
480-483-0991
7830 E Gelding DR
Scottsdale, AZ
Applied Computer Solutions
480-483-0162
7373 E Doubletree Ranch RD
Scottsdale, AZ
Lynk Software Inc
480-998-1933
PO Box 5498
Scottsdale, AZ
Lexcel Solutions
480-874-0443
4110 N Scottsdale RD
Scottsdale, AZ
Fmc Software Solutions
480-556-0800
7575 E Redfield Rd
Scottsdale, AZ
Pegasus Solutions Inc
480-624-6000
14000 N Pima RD
Scottsdale, AZ

provided by: 
Originally published at Internet.com


.NET Tip: Receive Notification When a File is Updated

Using the FileSystemWatcher class provides you with a very simple but flexible way to monitor the file system. It can be used to monitor directories, individual files, or groups of files. It also can be configured only to notify your application of certain types of changes. For this example, I'll show you a class that watches for changes in the LastWrite property of a specific file. I use this class to watch for changes in text files that contain the body of an email message that is sent when the status of your system changes. When one of these text files changes, the application can detect the change and cache the new contents of the file. Here is what the class looks like: public class MessageFileWatcher { public MessageFileWatcher(string Path, string FileName) { FileSystemWatcher Watcher = new FileSystemWatcher(); Watcher.Path = Path; Watcher.Filter = FileName; Watcher.NotifyFilter = NotifyFilters.LastWrite; Watcher.Changed += new FileSystemEventHandler(OnChanged); Watcher.EnableRaisingEvents = true; } private void OnChanged(object source, FileSystemEventArgs e) { // Do something here based on the change to the file } }

The constructor for my MessageFileWatcher class takes the path to the directory where the file is stored as well as the specific file name as parameters. A new FileSystemWatcher variable is created and the Path and Filter properties are set to the parameters passed into the constructor. Next, the NotifyFilter property is set to indicate the type of change which is of interest. In this case, I want to be notified every time the file is written, so I am using LastWrite. I then hook one of my methods into the Changed event of the FileSystem Watcher and enable it to raise events. I've included the OnChanged event handler for completeness, but the code in this method will depend upon your application. That is all there is to it. Here is an example usage of the MessageFileWatcher class: MessageFileWatcher WarningMessage = new MessageFileWatcher(@"C:\MyApp", "MyApp.Warning.msg"); MessageFileWatcher AlertMessage = new MessageFileWatcher(@"C:\MyApp", "MyApp.Alert.msg");

This provides a simple way to monitor the files used by your application. You may want to further explore the Filter and NotifyFilter properties as well as the various events available to make best use of the FileSystemWatcher class.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Applied Computer Solutions

480-483-0162
7373 E Doubletree Ranch RD
Scottsdale, AZ

Related Local Events
Toastmasters
Dates: 3/25/2010 - 3/25/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

Toastmasters
Dates: 5/6/2010 - 5/6/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

Toastmasters
Dates: 6/17/2010 - 6/17/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

Toastmasters
Dates: 7/29/2010 - 7/29/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details

Toastmasters
Dates: 9/9/2010 - 9/9/2010
Location: Peoria Chamber of Commerce
Peoria, AZ
View Details