Click here to Skip to main content
11,413,961 members (69,714 online)
Click here to Skip to main content

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 static Print() 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.

Comments and Discussions

GeneralMy vote of 1 Pin
Lavrekho, 15-Dec-12 5:23
memberLavrekho15-Dec-12 5:23 
Suggestionsource code is damaged Pin
Anand Priyadarshi, 27-Nov-12 22:14
memberAnand Priyadarshi27-Nov-12 22:14 
QuestionEvent not Firing Pin
michmsk, 19-Jan-12 3:55
membermichmsk19-Jan-12 3:55 
GeneralMy vote of 4 Pin
tusharpal, 17-Jul-11 19:51
membertusharpal17-Jul-11 19:51 
GeneralFile watcher results appended to a .csv file Pin
stacson, 2-Nov-10 9:41
memberstacson2-Nov-10 9:41 
GeneralMy vote of 3 Pin
bonypatel, 9-Aug-10 4:43
memberbonypatel9-Aug-10 4:43 
QuestionDisplay Username Pin
Holger_01, 30-Jun-08 7:45
memberHolger_0130-Jun-08 7:45 
Generalreport Pin
arkiboys, 25-Jun-08 2:47
memberarkiboys25-Jun-08 2:47 
GeneralRe: report Pin
arkiboys, 25-Jun-08 3:11
memberarkiboys25-Jun-08 3:11 
GeneralService Immediately Stops Pin
C1iff, 13-Mar-08 10:36
memberC1iff13-Mar-08 10:36 
Whenever I start the service it stops with an IO Exception. The Service's ErrorLogFile file cannot be accessed because the Service is using it?

Service cannot be started. System.IO.IOException: The process cannot access the file 'C:\FileSysWatcher\ErrorLogFile\Log.txt' because it is being used by another process.
at FileSysWatcher.Util.WriteToErrorLogFile(Exception sourceException)
at FileSysWatcher.FileSysWatcher.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

For more information, see Help and Support Center at
GeneralRe: Service Immediately Stops Pin
crazsmith, 14-Apr-08 12:59
membercrazsmith14-Apr-08 12:59 
GeneralRe: Service Immediately Stops Pin
Yuriy Medvedev, 24-Jul-08 21:05
memberYuriy Medvedev24-Jul-08 21:05 
Generalhey Pin
santoshkandhukuri, 28-Nov-07 13:28
membersantoshkandhukuri28-Nov-07 13:28 
QuestionReplace existing file Pin
Ronnie_CA, 5-Apr-07 9:04
memberRonnie_CA5-Apr-07 9:04 
GeneralCode problem, not working... Pin
shivaraj.gk, 28-Mar-07 2:24
membershivaraj.gk28-Mar-07 2:24 
GeneralLimitation of FSWatcher when used as a windows service Pin
Fayyaz Lodhi, 4-Jan-07 22:38
memberFayyaz Lodhi4-Jan-07 22:38 
GeneralRe: Limitation of FSWatcher when used as a windows service Pin
robertpocklington, 10-May-07 20:38
memberrobertpocklington10-May-07 20:38 
GeneralChange event fired multiple times Pin
Dave Guest, 1-Nov-06 6:08
memberDave Guest1-Nov-06 6:08 
Questionhow can i get the added text? Pin
tonghz, 14-Apr-06 3:42
membertonghz14-Apr-06 3:42 
GeneralSource Code Problem Pin
shofb, 5-Jan-06 8:26
membershofb5-Jan-06 8:26 
GeneralRe: Source Code Problem Pin
AVKDSiva, 10-Nov-06 2:54
memberAVKDSiva10-Nov-06 2:54 
GeneralUserName Pin
vicevice, 4-May-05 3:35
membervicevice4-May-05 3:35 
GeneralRe: UserName Pin
spidarin, 7-Feb-06 5:41
memberspidarin7-Feb-06 5:41 
GeneralRe: UserName Pin
Chris Byrd, 3-Jul-07 23:28
memberChris Byrd3-Jul-07 23:28 
GeneralRe: UserName Pin
meraydin, 5-Feb-07 12:10
membermeraydin5-Feb-07 12:10 
GeneralRe: UserName Pin
ghigbee, 16-Jul-07 12:25
memberghigbee16-Jul-07 12:25 
Questionhow to get notified while a file is opened? Pin
nzmomo, 27-Oct-04 20:12
membernzmomo27-Oct-04 20:12 
Questioncan i catch ...? Pin
nthevu, 4-Sep-04 9:10
membernthevu4-Sep-04 9:10 
AnswerRe: can i catch ...? Pin
Prashant B. Bhide, 30-Sep-04 0:27
memberPrashant B. Bhide30-Sep-04 0:27 
GeneralRe: can i catch ...? Pin
nthevu, 30-Sep-04 17:40
membernthevu30-Sep-04 17:40 
GeneralRe: can i catch ...? Pin
Paolo del Mundo, 14-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    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
Web01 | 2.8.150427.2 | Last Updated 2 Sep 2004
Article Copyright 2004 by Prashant B. Bhide
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid