Hey all,
I wrote a windows app in vb.Net that I had to recently convert to a windows service. I'm hoping somebody can tell where I went wrong because the code works in the windows app but doesn't do anything as a service. Below is the relevant code...(If Imiss anything - just let me know)
Service1.vb file:
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
'Add any initialization after the InitializeComponent() call
watchfolder = New System.IO.FileSystemWatcher()
'this is the path to the folder to be monitored
watchfolder.Path = "C:\ermdLocationTemperature"
'List of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
' Add the handler to each event - Only reacting to new files at creation
AddHandler watchfolder.Created, AddressOf logchange
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
End Sub
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 MyNewservice()}
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()
'
'MyNewservice
'
Me.ServiceName = "MyNewService"
End Sub
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.
getFolderFiles()
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
End If
'If a file has been newly created then...
If e.ChangeType = IO.WatcherChangeTypes.Created Then
oRead = oFile.OpenText(e.FullPath)
While oRead.Peek <> -1
parseTextFile(oRead)
End While
oRead.Close()
If blnDelete = True Then
oFile.Delete(e.FullPath)
End If
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
End If
getFolderFiles()
End Sub
Public Sub getFolderFiles()
Dim d() As String
Dim e As System.IO.FileSystemEventArgs()
Dim location As Object = "c:\ermdLocationTemperature"
d = System.IO.Directory.GetFiles(location)
Dim en As System.Collections.IEnumerator
en = d.GetEnumerator
While en.MoveNext
oRead = oFile.OpenText(en.Current)
parseTextFile(oRead)
oRead.Close()
If blnDelete = True Then
oFile.Delete(en.Current)
End If
End While
End Sub
I wrote a windows app in vb.Net that I had to recently convert to a windows service. I'm hoping somebody can tell where I went wrong because the code works in the windows app but doesn't do anything as a service. Below is the relevant code...(If Imiss anything - just let me know)
Service1.vb file:
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
'Add any initialization after the InitializeComponent() call
watchfolder = New System.IO.FileSystemWatcher()
'this is the path to the folder to be monitored
watchfolder.Path = "C:\ermdLocationTemperature"
'List of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
' Add the handler to each event - Only reacting to new files at creation
AddHandler watchfolder.Created, AddressOf logchange
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
End Sub
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 MyNewservice()}
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()
'
'MyNewservice
'
Me.ServiceName = "MyNewService"
End Sub
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.
getFolderFiles()
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
End If
'If a file has been newly created then...
If e.ChangeType = IO.WatcherChangeTypes.Created Then
oRead = oFile.OpenText(e.FullPath)
While oRead.Peek <> -1
parseTextFile(oRead)
End While
oRead.Close()
If blnDelete = True Then
oFile.Delete(e.FullPath)
End If
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
End If
getFolderFiles()
End Sub
Public Sub getFolderFiles()
Dim d() As String
Dim e As System.IO.FileSystemEventArgs()
Dim location As Object = "c:\ermdLocationTemperature"
d = System.IO.Directory.GetFiles(location)
Dim en As System.Collections.IEnumerator
en = d.GetEnumerator
While en.MoveNext
oRead = oFile.OpenText(en.Current)
parseTextFile(oRead)
oRead.Close()
If blnDelete = True Then
oFile.Delete(en.Current)
End If
End While
End Sub