views:

140

answers:

2

hi,

I have the following windows service file:

Imports System.ServiceProcess
Imports System.IO


Public Class fswService
Dim fsw As FileSystemWatcher
Dim lf As StreamWriter

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.
    lf = New StreamWriter(Application.StartupPath & "\fsw_lg.log")
    fsw = New FileSystemWatcher()

    fsw.Path = args(0)
    fsw.IncludeSubdirectories = True
    fsw.Filter = ".txt"
    fsw.EnableRaisingEvents = True

    AddHandler fsw.Created, New FileSystemEventHandler(AddressOf file_created)
    AddHandler fsw.Changed, New FileSystemEventHandler(AddressOf file_changed)
    AddHandler fsw.Deleted, New FileSystemEventHandler(AddressOf file_deleted)
End Sub

Public Sub file_created(ByVal obj As Object, ByVal e As FileSystemEventArgs)
    lf.WriteLine(Now.ToShortDateString & "-" & Now.ToShortTimeString & "-" & e.FullPath & "-created")
End Sub

Public Sub file_changed(ByVal obj As Object, ByVal e As FileSystemEventArgs)
    lf.WriteLine(Now.ToShortDateString & "-" & Now.ToShortTimeString & "-" & e.FullPath & "-changed")
End Sub

Public Sub file_deleted(ByVal obj As Object, ByVal e As FileSystemEventArgs)
    lf.WriteLine(Now.ToShortDateString & "-" & Now.ToShortTimeString & "-" & e.FullPath & "-deleted")
End Sub

Protected Overrides Sub OnStop()
    lf.Close() 
End Sub
End Class

i have the ServiceName set to fswService (same as class name). When I added an installer I also set the ServiceName for the ServiceInstaller1 as fswService.

I want to start this service at runtime based on the user setting the path of the folder to be watched. To achieve this I have the following:

Dim fsw_controller As New ServiceProcess.ServiceController
fsw_controller.Start(fswService)

2 problems: first, intellisense error saying: 'fswService' is a type and cannot be used as an expression. second, I can not figure out a way to pass on to the service the path of the folder to watch (which is stored at My.Settings.userPath).

I really thought this is how you start a service. Am I missing something?

Your help is, as always, appreciated. Thanks

A: 

ok. I think I figured them both out but I am getting an error when I run the application.

In the service file I changed

fsw.Path = args(0)

To

fsw.Path = My.Settings.userPath

To fix starting the service (as I mentioned in my comment) I did:

Dim fsw_controller As New ServiceController("fswService")
fsw_controller.Start()

i rebuilt the application, re-installed the service using installutil.exe and ran it. But I am getting an error:

InvalidOperationException was unhandled
Cannot start service fswService on computer '.'.

Any help with that?? Please..

mazrabul
A: 

You can only send an integer value (between 128-255) through the ServiceController.ExecuteCommand procedure. You then override the OnCustomCommand event in your Windows Service, which runs when the ExecuteCommand is sent to that specific service. I would save the name of the folder to be watched in a text file and just have the OnCustomCommand event look into that file for the folder. You are probably getting that error because you ran the Start procedure of the ServiceController when the Windows Service was already running. To update the ServiceController.Status property, run the StatusController.Refresh() procedure.

DaMartyr