473,795 Members | 3,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ServiceP rocess.ServiceB ase

Public folderToWatch As New FileSystemWatch er

#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeCompo nent()
' Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase
' 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.ServiceP rocess.ServiceB ase () {New Service1,
New MySecondUserSer vice}
'
ServicesToRun = New System.ServiceP rocess.ServiceB ase() {New ScanFolder}
System.ServiceP rocess.ServiceB ase.Run(Service sToRun)
End Sub
'Required by the Component Designer
Private components As System.Componen tModel.IContain er
' 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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
'
'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 FileSystemWatch er
' strSysDir = System.Environm ent.SystemDirec tory()
strSysDir = "C:\Test"

folderToWatch.P ath = strSysDir

With folderToWatch
.NotifyFilter = .NotifyFilter Or NotifyFilters.F ileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.A ttributes
' To watch sub directories....
.IncludeSubdire ctories = True
End With

AddHandler folderToWatch.C reated, AddressOf newLog
AddHandler folderToWatch.D eleted, AddressOf newLog
folderToWatch.E nableRaisingEve nts = True

End Sub

Private Sub newLog(ByVal Source As Object, ByVal evt As FileSystemEvent Args)
If evt.ChangeType = WatcherChangeTy pes.Created Then
writeToLog(evt. FullPath, "Created")
ElseIf evt.ChangeType = WatcherChangeTy pes.Deleted Then
writeToLog(evt. FullPath, "Deleted")
End If
End Sub

Private Sub writeToLog(ByVa l filePath As String, ByVal fileManip As String)
Dim evtLog As New EventLog
If Not evtLog.SourceEx ists("ScanFolde r") Then
evtLog.CreateEv entSource("Scan Folder", "Log of ScanFolder")
End If

evtLog.Source = "ScanFolder "
evtLog.WriteEnt ry("ScanFolder Log", "File " & filePath & " has " &
fileManip & " by " & Environment.Use rName.ToString,
EventLogEntryTy pe.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 1364
[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
3497
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, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert statements it generates. Could anyone please help me?
1
1955
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 past 2 weeks now i give up and call for help. Please if anyone can gide me through i will be so grateful!! I have pasted my code below
7
3616
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> </head> <style type="text/css">
6
3026
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 an html authoring tool to help you accomplish? 2. What do you expect from online help for a html authoring tool? 3. What audience do you think a freeware html authoring tool is directed towards?
5
3002
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 <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
23
3290
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 to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
13
4337
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 "operation is not allowed when object is open" so I take out the line of code: BookDetails.Connection1.Open and it comes up with the error "operation is not allowed when object
2
1889
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 anyone can help me please email me at tuan_vandyk@yahoo.co.uk Thanks
1
2291
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 button the list is populated with a list of that genre. I have got the interface done to satisfaction, my problem is that when i press the change genre button nothing happens and when i select a track to play from the list which is setvisible and...
5
2322
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 for user name, check in the system but does not return the correct answer please help me with it. or if you have better way of doing it would you please mind to tell me.. thanks.. #!/usr/bin/perl -w #use Getopt::Std;
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.