.NET Tip: Receive Notification When a File is Updated Houston TX

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

Entrance Software
713-357-4930
1001 McKinney
Houston, TX
ObjectWin Technology, Inc.
(713) 337-1834
2650 Fountain View Drive, Suite 405
Houston, TX
Delta Business Solutions
(281) 445-3600
525 N. Sam Houston PKWY E
Houston, TX
Silicus Techologies
(713) 353-7416
Gray Falls
Houston, TX
Palindrome Software Labs Ltd
206-366-5542
7700 WillowChase BLVD
Houston, TX
NuHire Recruiting Solutions
281-748-9990
17002 Nautical Pointe Ln
Houston, TX
On Center Software
(281) 875-6829
222 Pennbright Dr
Houston, TX
Dataface Inc
(713) 942-0485
Houston, TX
Allied Henderson
(713) 957-1661
2000 W Governors Cir
Houston, TX
Mapper Solution Center
(281) 345-2855
16206 Waiting Spring Cir
Houston, TX

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

Entrance Software

Entrance Software is a Software Development and Consulting company and a Microsoft Gold Certified Partner.

713-357-4930
1001 McKinney
Houston, TX
http://www.entrancesoftware.com

For mid-sized businesses that own or need software, Entrance Software is a Trusted Advisor. Unlike other contract programming or staffing companies, Entrance exclusively provides Professional Software Consultants who are not only technical geniuses, but also understand the business value of Sales, Profit, Productivity, Loyalty and Morale.

Entrance Software is a Microsoft Gold Certified Partner and develops custom software applications for businesses in a variety of industries including Oil and Gas, Exploration and Production, Manufacturing, Education, Legal and Medical Services.


Related Local Events
Deep Offshore Technology (DOT 2010)
Dates: 2/2/2010 - 2/4/2010
Location: George R. Brown Convention Center
Houston, TX
View Details

Subsea Tieback Exhibition (SSTB)
Dates: 3/2/2010 - 3/4/2010
Location: Moody Gardens Hotel & Convention Center
Galveston, TX
View Details

2009 IEEE Industry Applications Society Annual Meeting
Dates: 10/4/2009 - 10/7/2009
Location: Hyatt Regency Houston
Houston, TX
View Details