Click here to Skip to main content
6,632,253 members and growing! (20,117 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

File Watcher Windows Service in C#

By Prashant B. Bhide

This article will give you a peep into creating a basic windows service in C#
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:1 Sep 2004
Views:93,803
Bookmarked:61 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 3.34 Rating: 3.00 out of 5
4 votes, 30.8%
1
1 vote, 7.7%
2
2 votes, 15.4%
3

4
6 votes, 46.2%
5

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

About the Author

Prashant B. Bhide


Member
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.
Occupation: Web Developer
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
QuestionDisplay Username PinmemberHolger_017:45 30 Jun '08  
Generalreport Pinmemberarkiboys2:47 25 Jun '08  
GeneralRe: report Pinmemberarkiboys3:11 25 Jun '08  
GeneralService Immediately Stops PinmemberC1iff10:36 13 Mar '08  
GeneralRe: Service Immediately Stops Pinmembercrazsmith12:59 14 Apr '08  
GeneralRe: Service Immediately Stops PinmemberYuriy Medvedev21:05 24 Jul '08  
Generalhey Pinmembersantoshkandhukuri13:28 28 Nov '07  
QuestionReplace existing file PinmemberRonnie_CA9:04 5 Apr '07  
GeneralCode problem, not working... Pinmembershivaraj.gk2:24 28 Mar '07  
GeneralLimitation of FSWatcher when used as a windows service PinmemberFayyaz Lodhi22:38 4 Jan '07  
GeneralRe: Limitation of FSWatcher when used as a windows service Pinmemberrobertpocklington20:38 10 May '07  
GeneralChange event fired multiple times PinmemberDave Guest6:08 1 Nov '06  
Generalhow can i get the added text? Pinmembertonghz3:42 14 Apr '06  
GeneralSource Code Problem Pinmembershofb8:26 5 Jan '06  
GeneralRe: Source Code Problem PinmemberAVKDSiva2:54 10 Nov '06  
GeneralUserName Pinmembervicevice3:35 4 May '05  
GeneralRe: UserName Pinmemberspidarin5:41 7 Feb '06  
GeneralRe: UserName PinmemberChris Byrd23:28 3 Jul '07  
GeneralRe: UserName Pinmembermeraydin12:10 5 Feb '07  
GeneralRe: UserName Pinmemberghigbee12:25 16 Jul '07  
Generalhow to get notified while a file is opened? Pinmembernzmomo20:12 27 Oct '04  
Generalcan i catch ...? Pinmembernthevu9:10 4 Sep '04  
GeneralRe: can i catch ...? PinmemberPrashant B. Bhide0:27 30 Sep '04  
GeneralRe: can i catch ...? Pinmembernthevu17:40 30 Sep '04  
GeneralRe: can i catch ...? PinmemberPaolo del Mundo5:09 14 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Sep 2004
Editor: Nishant Sivakumar
Copyright 2004 by Prashant B. Bhide
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project