By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,189 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,189 IT Pros & Developers. It's quick & easy.

Bit of help with this code please?

P: n/a
Hi all,

I found an article on how to write a Windows service here:

It was pretty much what I wanted, I want the service to scan a directory and
do something based on a change - in my case I will want to point it to the
inetpub/mailbox directory and look for new emails but that aside the process
seemed the same.

I've put the example in - I had a few problems getting stuff in the write
places initially but seemed to get it all error free, I added the installer,
built it, installed it and started it - whilst now there are now errors it
doesn't appear to do anything when I add/change/remove a file from the
directory specified.

The problem I have is that I am not sure if...

a) The article is correct
b) The "something happening" detection is failing
c) The writing to the log file is failing

As I say there are no errors in the code according to VS, and there are no
errors generated when the service is running - it just doesn't seem to do
anything - I may have misinterpreted what it was supposed to do of course,
my expectations were that it would create a new log file in the event
viewer, and then write rows to it upon files being added/changed/removed
from a specific directory.

If anyone can help me with this I would be very grateful - very new to .Net
and this is the first "windows" thing I've tried, the rest have been web

I have put the final code from my service below.

I thank you for your time and any help you can offer.



PS: There might be a couple of commented bits in there referencing
'service1' that was the name of the service before i changed it to something
more appropriate 'ScanFolder' etc
**** code ****

Imports System.IO

Public Class ScanFolder
Inherits System.ServiceProcess.ServiceBase

Public folderToWatch As New FileSystemWatcher

#Region " Component Designer generated code "
Public Sub New()
' This call is required by the Component Designer.
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
End If
End If
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1,
New MySecondUserService}
ServicesToRun = New System.ServiceProcess.ServiceBase() {New ScanFolder}
System.ServiceProcess.ServiceBase.Run(ServicesToRu n)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ServiceName = "ScanFolder"
End Sub
#End Region

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim strSysDir As String
' folderToWatch = New FileSystemWatcher
' strSysDir = System.Environment.SystemDirectory()
strSysDir = "C:\Test"

folderToWatch.Path = strSysDir

With folderToWatch
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.Attributes
' To watch sub directories....
.IncludeSubdirectories = True
End With

AddHandler folderToWatch.Created, AddressOf newLog
AddHandler folderToWatch.Deleted, AddressOf newLog
folderToWatch.EnableRaisingEvents = True

End Sub

Private Sub newLog(ByVal Source As Object, ByVal evt As FileSystemEventArgs)
If evt.ChangeType = WatcherChangeTypes.Created Then
writeToLog(evt.FullPath, "Created")
ElseIf evt.ChangeType = WatcherChangeTypes.Deleted Then
writeToLog(evt.FullPath, "Deleted")
End If
End Sub

Private Sub writeToLog(ByVal filePath As String, ByVal fileManip As String)
Dim evtLog As New EventLog
If Not evtLog.SourceExists("ScanFolder") Then
evtLog.CreateEventSource("ScanFolder", "Log of ScanFolder")
End If

evtLog.Source = "ScanFolder"
evtLog.WriteEntry("ScanFolder Log", "File " & filePath & " has " &
fileManip & " by " & Environment.UserName.ToString,

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub

End Class
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a

I have this working now - yay!

Next question - in the Service window (from Administrative Tools) I see the
service, I see its name, its state and the type of start up - yet my service
has no description? Anyone know how I can add a description for it via the

Any help appreciated,


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.