Click here to Skip to main content
12,066,448 members (57,551 online)
Click here to Skip to main content
Add your own
alternative version


87 bookmarked

File Watcher Windows Service in C#

, 1 Sep 2004
Rate this:
Please Sign up or sign in to vote.
This article will give you a peep into creating a basic windows service in C#


FileSyswatcher is a windows service that monitors a specified path on your disk drive. It will check for all changes to a directory including it's subdirectories and files. The application enables you :

  • To keep check on changes made to directories.
  • Log which user made what changes to which directories and files on what date and time.
  • Generate a HTML report for the daily logs.

FileSysWatcher is written in C# and .Net 1.1. FileSysWatcher will run on all MS Windows platforms that support Windows Services. It's a Windows Service application that uses many powerful features of the .NET platform.


I developed this application to demonstrate how to create a windows service. It also demonstrates the use of the FileSystemWatcher object. It demonstrates how to catch various file change events such as Delete, Create, Rename and Change. Other feature's used are XML, XSLT, FileStream and StreamReader and StreamWriter of the XML and IO namespaces respectively.

Using the code

The application has the following files:
  1. bin\Release\FileSysWatcher.exe.config
  2. bin\Release\logStyles.xsl
  3. App.config
  4. FileSysWatcher.cs
  5. PrintReport.cs
  6. ProjectInstaller.cs
  7. Util.cs bin\Release\FileSysWatcher.exe.config:

This file is generated every time you compile the application. Its a copy of the App.Config file. This is the application configuration file where I have added key-value pairs.


This is the xsl file which is used to generate the HTML report from the xml.


This is the application configuration file where I have added key-value pairs. When application is compiled it generates a file called FileSysWatcher.exe.config which is copied to the bin\Release folder. It contains following values:

  • LOGFILENAME file path where log file is created.
  • ERRORLOGFILENAME file path where errors during execution are logged.
  • PATHTOWATCH the path to monitor.


This is the actual service file. It has the FileSysWatcher class which inherits from the ServiceBase class. It has methods such as,

  • OnStart()
  • OnStop()

It also hosts the FileSystemWatcher object which exposes the following events,

  • FSWatcher_Changed()
  • FSWatcher_Created()
  • FSWatcher_Deleted()
  • FSWatcher_Renamed()

These events are fired everytime a file/directory is changed, deleted, renamed or created.


This contains a staticPrint() method to generate the HTML report.


This is a ServiceInstaller which is autogenerated. No code has been modified in this class.


This contains utility methods.

Points of Interest

Some Interesting points:

  • Generating HTML using XML and XSLT.
  • Monitoring File and Directory Activity
  • Installing and UnInstalling a Windows Service.


  • First Update : 02nd September 2004.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Prashant B. Bhide
Web Developer
India India
A Microsoft Certified Professional with over 2 Yrs experience in .Net. Worked on both Windows-based and Web-based Technologies. Used Cutting Edge Tools such as Visual Studio.Net 2003, Oracle9i. Worked with organisations such as Cygnus Software Pvt. Ltd. and Deloitte Consulting. Presently working with Nihilent Technologies Pvt. Ltd., Pune, India as System Analyst.

You may also be interested in...

Comments and Discussions

GeneralRe: UserName Pin
ghigbee16-Jul-07 12:25
memberghigbee16-Jul-07 12:25 
Questionhow to get notified while a file is opened? Pin
nzmomo27-Oct-04 20:12
membernzmomo27-Oct-04 20:12 
Questioncan i catch ...? Pin
nthevu4-Sep-04 9:10
membernthevu4-Sep-04 9:10 
AnswerRe: can i catch ...? Pin
Prashant B. Bhide30-Sep-04 0:27
memberPrashant B. Bhide30-Sep-04 0:27 
GeneralRe: can i catch ...? Pin
nthevu30-Sep-04 17:40
membernthevu30-Sep-04 17:40 
GeneralRe: can i catch ...? Pin
Paolo del Mundo14-Oct-04 5:09
memberPaolo del Mundo14-Oct-04 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 2 Sep 2004
Article Copyright 2004 by Prashant B. Bhide
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid