Step 3 – Add logging

Step 4 – Implement the Service

Right-click on the Service1.cs file and choose View Code.

Change both the Name and the ServiceName to DirectoryMonitoringService. You can right-click on the file to rename. If that doesn’t rename the class, you can open the file, right-click on the class name and choose Refactor | Rename.

Go to the code of the DirectoryMonitoringService.cs file (which was Service1.cs just a couple steps ago) in Visual Studio.

This is a known bug. I'm sure if you google around you can find it.
I had to detect the first one, set a value to true (_IsProcessing) so that the second one didn't fire. Then I had to enhance it so the _IsProcessing only worked for the one file, in case two files were modified at a time. I think I used a dictionary, added the file, then on completion removed the file.

Thank you for this great tutorial, helped me a bunch creating a custom service for my self.

I have one problem though, before I made this service I made a similar windows forms application doing all the actions which I've put in my service project, but everytime a new file is put in the watch folder it does it's job and stops the service.
this isn't the way it should be is it?
any Ideas on how to fix this?

It's a nice Article. Could you also tell me if we can monitor the folder for a certain amoutn of time. Let's take an example: I want to monitor the folder/Directory continuously if there is nothing happening for 10 mins i want to raise an alert. Is that possible? If yes please let me know.

It will take work. There will not be an event automatically raised that I know of, so you will have to create a timer thread yourself and raise an event yourself. Create a Dictionary where the string is the path to the folder and the Date is the last changed time. Then have your timer thread check ever n seconds, where n is configurable. Maybe you only check every 10 minutes so n is 600.

You probably have the drive mapped so you think the service has access to that share. Unfortunately that is not the case. Only your currently logged in user has access to the share. When the service runs, it runs as LocalSystem. LocalSystem does not have access to shares that you as a user have authenticated too.

My other blogs

Entries (RSS) and Comments (RSS). Copyright ® Rhyous.com
Linking to content on this site is allowed without permission and as many as ten lines of any article can be used along with such link.
Any other use of the content is allowed only by permission of Rhyous.com.