Click here to Skip to main content
13,452,126 members (51,873 online)
Click here to Skip to main content
Add your own
alternative version


78 bookmarked
Posted 7 Aug 2005


, 7 Aug 2005
Rate this:
Please Sign up or sign in to vote.
A Windows service which monitors the directory changes, writes an entry in the event log about the change, notifies the change to the users by sending a mail, and also converts the input XML file into a DataSet.


When I was reading the MSDN, I found an interesting topic on Windows services and FileSystemWatcher. So I tried to write a Windows service which monitors the directory changes, writes an entry in the event log about the change, notifies the changes to the users by sending mail, and also converts the input XML file into a DataSet.

This service is useful in scenarios where the clients upload the files (say XML files) on the server and the server will take care of processing the uploaded files and update them in the database if they are valid.

Building the Application

Start by creating a new Windows service project:

Change the service1.cs name to XMLWatcher.cs by editing the service1.cs properties in the Solution Explorer and also change service1 to XMLWatcher in the generated view code window.

This application uses FileSystemWatcher to observe the directory changes and SmtpMail to send the mails. To access FileSystemWatcher and MailMessage (in SmtpMail), we need to add references to System.IO and System.Web.dll to this project using Project->Add Reference. We should also add a reference to System.Data.dll to access ADO.NET objects.

Include System.IO, System.Web.Mail and System.Data.SqlClient namespaces to this project.

using System.IO;
using System.Web.Mail;
using System.Data.SqlClient;

Now write the code to initialize the Event Log, FileSystemWatcher and mail server in InitializEventLog(), IntializeFileSystemWatcher() and InitializeMailServer() respectively, and call them from OnStart();

/// <summary> 
/// Initialize the Event Log
/// </summary>
private void InitializEventLog()
   //Check whether " XMLWatcherSource " exist or not
     //Create "XMLWatcherSource" and "XMLWatcherLog"
   //Create Event Log
   el=new EventLog();
    //Assign Event Log Name
   //Assign Event Source Name
/// <summary> 
/// Initialize File System Watcher
/// </summary>
private void IntializeFileSystemWatcher()
  //Create File System Watcher for XML files
   fsWatcher=new System.IO.FileSystemWatcher("c:\\temp","*.xml");
  // Add event handlers for new XML files and change of existing XML files.
   fsWatcher.Changed += new FileSystemEventHandler(OnXMLFileChanged);
   fsWatcher.Created += new FileSystemEventHandler(OnXMLFileCreated);
   // Begin watching.
   fsWatcher.EnableRaisingEvents = true;


/// <summary> 
/// Initalize Mail Server
/// </summary>
private void InitializeMailServer()
    mailMsg=new System.Web.Mail.MailMessage();

/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
    // TODO: Add code here to start your service.

    //Initialize Event Log
    //Initialize File System Watcher
    //Initliaze Mail Server


Now provide Event Handlers for FileSystemWatcher. These Event Handlers get called when a file is changed or created. Event Handlers will write the entry into Event Log and notify the required people by sending a mail and also converts the XML data into a DataSet. This DataSet can be used to update the database.

/// <summary> 
/// Event Handler for File Changed 
private void OnXMLFileChanged(object source, FileSystemEventArgs e)
    //Write entry into the Event Log
    el.WriteEntry("XML File :" + e.FullPath + " changed");

    //Send mail 

    //Get the Dataset from XML

/// <summary> 
/// Event Handler for File Created 
/// </summary>
private void OnXMLFileCreated(object source, FileSystemEventArgs e)
    //Write entry into the Event Log
    el.WriteEntry("XML File :" + e.FullPath + " created");

    //Send mail 

//Get the Dataset from XML


/// <summary> 
/// Notify the users by sendig mail
/// </summary>
private void SendMail(string XMLFileName)
    string fileName=XMLFileName;
    //Message From 
    //Message To
    //Message Subject
    mailMsg.Subject="New File Uploaded to the server ";
    //Message Body
    mailMsg.Body="XML File :" + fileName + " is uploaded ";
    //Everything send the mail


/// <summary> 
/// Get Dataset from XML 
/// </summary>
private DataSet GetDataSetFromXML(string XMLFileName)
    string uploadedXMLfile = XMLFileName;
    //Create the Dataset
    DataSet ds = new DataSet();
    // Create new FileStream with which to read the schema.
    System.IO.FileStream fsReadXml = new System.IO.FileStream
                (uploadedXMLfile, System.IO.FileMode.Open);
    catch (Exception ex)
        //Write entry into the Event Log
        el.WriteEntry("Error in readig XML File : " 
                               + uploadedXMLfile );
    return ds;


We have done with the coding. Now this service needs to be installed under the services. To install this service, we need to add installers to this project.

To add installers to the project, open XMLWatcher.cs in design mode and right click on the form and click Add Installer.

Visual Studio will add ServiceProcessInsatller1 and ServiceInsatller1 to the project.

Right click on the ServiceProcessInsatller1 properties and change the Account to Local System to avoid user name, password entry while installing the service.

Now build the project. After build succeeds, open Visual Studio .NET 2003 Command Prompt. Using installutil command, install the service.

Now the service is installed under the Services. To start the service, go to Administrative Tools->Services, and look for XMLWatcherservice and start the service.

Now the service is up and running. It's time to test the service.

To test the service, create an XML file in c:\temp. Service will monitor the directory change and write an entry in the Event Log under XMLWatcherLog. It will also send a mail to the required users about the directory change.

This application can be further extended to insert the DataSet (which is retrieved from GetDataSetFromXML) into the database.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Venkata Kancharla
United States United States
He is graduated from IIT Madras and has got software design and development experience in C#,ASP.NET,C++,COM,COM+,MFC,Bootstrap,MVC,JQuery,WCF and SQL Server .

You may also be interested in...

Comments and Discussions

GeneralNice Article Pin
master vicky20-Dec-07 23:39
membermaster vicky20-Dec-07 23:39 
QuestionChange event Pin
moko202019-May-07 2:48
membermoko202019-May-07 2:48 
GeneralGreat work. Mail is not sending. Pin
psubbarao18-Nov-06 2:02
memberpsubbarao18-Nov-06 2:02 
GeneralRe: Great work. Mail is not sending. Pin
Venkata Kancharla20-Nov-06 5:51
memberVenkata Kancharla20-Nov-06 5:51 
QuestionNetwork Path on Linux Pin
lipingffeng7-Aug-06 15:36
memberlipingffeng7-Aug-06 15:36 
GeneralSource Not Found Pin
| Muhammad Waqas Butt |25-May-06 4:48
member| Muhammad Waqas Butt |25-May-06 4:48 
GeneralGreat work! Pin
Pi po12-Apr-06 20:45
memberPi po12-Apr-06 20:45 
GeneralRe: Great work! Pin
Venkata Kancharla13-Apr-06 4:04
memberVenkata Kancharla13-Apr-06 4:04 
GeneralThe code Pin
Goonja25-Aug-05 2:49
sussGoonja25-Aug-05 2:49 
GeneralRe: The code Pin
Anonymous25-Aug-05 10:35
sussAnonymous25-Aug-05 10:35 
GeneralRe: The code Pin
Venkata Kancharla25-Aug-05 10:36
memberVenkata Kancharla25-Aug-05 10:36 
GeneralVery nice Pin
Rick Crone11-Aug-05 6:02
memberRick Crone11-Aug-05 6:02 
GeneralRe: Very nice Pin
Venkata Kancharla11-Aug-05 6:42
memberVenkata Kancharla11-Aug-05 6:42 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180318.3 | Last Updated 8 Aug 2005
Article Copyright 2005 by Venkata Kancharla
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid