Windows Service using File System Watcher

  • Thread starter Thread starter Troy Murphy
  • Start date Start date
T

Troy Murphy

I seem to remember using a walkthru in the early beta that demonstrated how
to create a Windows Service that would monitor events in a folder.
Can someone please direct me to an example of doing this?

Thanks,
Troy
 
Thank you!
I will attempt to implement your guidance.

Troy


Ignacio Machin said:
Hi Troy,

Here are codes snipets I'm using

protected override void OnStart(string[] args)

{

// TODO: Add code here to start your service.

//Start the listener thread

listenerThread = new Thread( new ThreadStart( ListenerMethod));

listenerThread.Start();

}

protected void ListenerMethod()

{

//In this form we have no control over the children threads

Thread workingthread;

//Create the FileWatcher objects

SetWatchers();

// ...... trimmed
}

// This function created the watchers , one of then watch the config file of
the service, so I can change settings without stop/start the service. !!!!
public void SetWatchers()

{

try

{

Clientwatcher = new FileSystemWatcher();

Configwatcher = new FileSystemWatcher();


Clientwatcher.Path = Config.StorageFolder;

Clientwatcher.Filter = Config.ClientExportFile;

Configwatcher.Path = Config.StorageFolder;

Configwatcher.Filter = Config.ConfigExportFile;

/* Watch for changes in LastAccess and LastWrite times, and

the renaming of files or directories. */

Clientwatcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite ;



Configwatcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite ;



// Add event handlers.

Clientwatcher.Created += new FileSystemEventHandler( ClientFileUpdated);

Clientwatcher.Changed += new FileSystemEventHandler( ClientFileUpdated);



Configwatcher.Created += new FileSystemEventHandler( ConfigFileUpdated);

Configwatcher.Changed += new FileSystemEventHandler( ConfigFileUpdated);

//Activate them

Configwatcher.EnableRaisingEvents =

Productwatcher.EnableRaisingEvents = Clientwatcher.EnableRaisingEvents =
true;

}

catch( Exception e)

{

eventLog1.WriteEntry( "Error in SetWatchers : " + e.Message,
EventLogEntryType.Error);

}

}

// Now all you have to do is create the handlers.


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Troy Murphy said:
I seem to remember using a walkthru in the early beta that demonstrated how
to create a Windows Service that would monitor events in a folder.
Can someone please direct me to an example of doing this?

Thanks,
Troy
 
Back
Top