473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bit of help with this code please?

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(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()
'
'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
Nov 18 '05 #1
1 1340
[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
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql,...
1
by: the_proud_family | last post by:
HELP ME PLEASE!! my email is the_proud_family@yahoo.com I can't get the ball to go up right side and then I need it to turn around and keep turning until velocity=0 I have been at it for the ...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
2
by: tuan_vandyk | last post by:
Hi I desperately need help with my project. Theoretically everything should work bu it just isn't. Please email me for a copy of the project's source code. It was made in Turbo C++ 5. Please if...
1
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.