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, FileStr
eam and
StreamReader
and
StreamWriter
of the XML and IO namespaces respectively.
Using the code
The application has the following files:
- bin\Release\FileSysWatcher.exe.config
- bin\Release\logStyles.xsl
- App.config
- FileSysWatcher.cs
- PrintReport.cs
- ProjectInstaller.cs
- 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,
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.