Bit of help with this code please?

R

Rob Meade

Hi all,

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

http://www.dotnetbips.com/displayarticle.aspx?id=178

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
apps...

I have put the final code from my service below.

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

Regards

Rob

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()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' 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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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(ServicesToRun)
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()
'
'Service1
'
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,
EventLogEntryType.Information)

End Sub

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

End Class
 
R

Rob Meade

[snip]

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
code?

Any help appreciated,

Regards

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top