Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C#
Article

File Watcher Windows Service in C#

Rate me:
Please Sign up or sign in to vote.
3.26/5 (20 votes)
1 Sep 20042 min read 230.9K   8.2K   88   31
This article will give you a peep into creating a basic windows service in C#

Image 1

Introduction

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.

Background

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.

bin\Release\logStyles.xsl:

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

App.config:

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.

FileSysWatcher.cs:

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.

PrintReport.cs:

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

ProjectInstaller.cs:

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

Util.cs:

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.

History

  • First Update : 02nd September 2004.

License

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


Written By
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
Lavrekho15-Dec-12 4:23
Lavrekho15-Dec-12 4:23 
Suggestionsource code is damaged Pin
Anand Priyadarshi27-Nov-12 21:14
Anand Priyadarshi27-Nov-12 21:14 
QuestionEvent not Firing Pin
michmsk19-Jan-12 2:55
michmsk19-Jan-12 2:55 
GeneralMy vote of 4 Pin
_Tushar Patil17-Jul-11 18:51
_Tushar Patil17-Jul-11 18:51 
GeneralFile watcher results appended to a .csv file Pin
stacson2-Nov-10 8:41
stacson2-Nov-10 8:41 
GeneralMy vote of 3 Pin
bonypatel9-Aug-10 3:43
bonypatel9-Aug-10 3:43 
QuestionDisplay Username Pin
Holger_0130-Jun-08 6:45
Holger_0130-Jun-08 6:45 
Generalreport Pin
arkiboys25-Jun-08 1:47
arkiboys25-Jun-08 1:47 
GeneralRe: report Pin
arkiboys25-Jun-08 2:11
arkiboys25-Jun-08 2:11 
GeneralService Immediately Stops Pin
C1iff13-Mar-08 9:36
C1iff13-Mar-08 9:36 
GeneralRe: Service Immediately Stops Pin
crazsmith14-Apr-08 11:59
crazsmith14-Apr-08 11:59 
GeneralRe: Service Immediately Stops Pin
Yuriy Medvedev24-Jul-08 20:05
Yuriy Medvedev24-Jul-08 20:05 
Generalhey Pin
santoshkandhukuri28-Nov-07 12:28
santoshkandhukuri28-Nov-07 12:28 
QuestionReplace existing file Pin
Ronnie_CA5-Apr-07 8:04
Ronnie_CA5-Apr-07 8:04 
GeneralCode problem, not working... Pin
shivaraj.gk28-Mar-07 1:24
shivaraj.gk28-Mar-07 1:24 
GeneralLimitation of FSWatcher when used as a windows service Pin
Fayyaz Lodhi4-Jan-07 21:38
Fayyaz Lodhi4-Jan-07 21:38 
I have a problem relating to Windows Services and FileSystemWatcherClass of .NET.

I developed a windows service to monitor a specific folder on a server for changes and maintains its updated back up in another folder.

FileSystemWatcherClass is inherited to monitor and create mirror of a folder. It is available at http://www.codeproject.com/cs/files/directorymirror.asp?msg=1801877.

However, I found a limitation of this application. When copying large data (10 – 15 MB) it misses many events and only copies about half of the data. (This problem is also referred in the comments of the above article).

Possible cause of the problem:
When you copy or drop large amounts of data somewhere it can take several seconds for the system to write the data to the disk. The system cannot send feedback about the status of the operation (completed, failed, queued, etc) while it is performing it or while it is queued, and during this time other requests to the file system are queued by the system. This is what I think is the source of the problem: The FileSystemWatcher has no patience and throws an exception if it doesn’t get an IMMEDIATE response from the file system. So any big operation that takes a bit of time will end up throwing an exception and often but not always, cause the operation to abort. This class seems to be completely ignorant of how the windows file system works.


I found a solution to that on msdn, that says increase the INTERNALBUFFERSIZE. The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification.

This solution resolved the problem somehow. I increased buffer to 64KB (default is 8192 bytes). This successfully copied data up to 35MB without missing a single file and suffices to my needs.

The problem is, when I integrated this into a Windows Service, it all failed to copy even 3 – 4 MB of Data. I have searching a lot but haven’t found the right solution yet.

I would be grateful if somebody could help me out with that. Thanks a lot for your patience of reading a long mail.

regards,

Fayyaz Khan Lodhi
fayyaz.lodhi@confiz.com

GeneralRe: Limitation of FSWatcher when used as a windows service Pin
robertpocklington10-May-07 19:38
robertpocklington10-May-07 19:38 
GeneralChange event fired multiple times Pin
Dave Guest1-Nov-06 5:08
Dave Guest1-Nov-06 5:08 
Questionhow can i get the added text? Pin
tonghz14-Apr-06 2:42
tonghz14-Apr-06 2:42 
GeneralSource Code Problem Pin
shofb5-Jan-06 7:26
shofb5-Jan-06 7:26 
GeneralRe: Source Code Problem Pin
AVKDSiva10-Nov-06 1:54
AVKDSiva10-Nov-06 1:54 
GeneralUserName Pin
vicevice4-May-05 2:35
vicevice4-May-05 2:35 
GeneralRe: UserName Pin
spidarin7-Feb-06 4:41
spidarin7-Feb-06 4:41 
GeneralRe: UserName Pin
Chris Byrd3-Jul-07 22:28
Chris Byrd3-Jul-07 22:28 
GeneralRe: UserName Pin
meraydin5-Feb-07 11:10
meraydin5-Feb-07 11:10 

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.