G
Guest
I have written a service, but it won't stop, Eventvwr reports the following:
"Failed to stop service"
The code is as follows:
============================================
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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String
Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\DeleteFolders", True)
strFoldersFromRegistry = MyKey.GetValue("FolderList")
strIntervalFromRegistry = MyKey.GetValue("Interval")
'Split the folder list into an array of individual entries
strFolderList = Split(strFoldersFromRegistry, ";")
'Start the timer and set the interval to the correct number of
milliseconds
Dim dblTimerInterval As Double
dblTimerInterval = CDbl(strIntervalFromRegistry)
Timer1.Interval = (dblTimerInterval * 1000)
Timer1.Start()
EventLog1.WriteEntry("Delete Folders", "DeleteFolders is now
monitoring " & strFoldersFromRegistry)
End Sub
====================================================
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
booRunning = False
'Stop the timer
Timer1.Stop()
EventLog1.WriteEntry("DeleteFolders is no longer monitoring folders")
End Sub
===================================================
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Do While booRunning = True
'Do the "Delete Folders" routine for each folder in the list
Dim strCurrentFolder As String
Dim MyFolder As New FileSystemObject
For Each strCurrentFolder In strFolderList
If Len(strCurrentFolder) = 0 Then Exit Sub
'Check the folder exists
If MyFolder.FolderExists(strCurrentFolder) = True Then
DeleteEmptyFolders(strCurrentFolder)
End If
Next
Loop
End Sub
====================================================
Private Sub DeleteEmptyFolders(ByVal strCurrentFolder As String)
Dim fsoSubFolders As Folders
Dim fsoSubFolder As Folder
Dim fsoFolder As Folder
Dim strPaths()
Dim lngFolder As Long
Dim lngSubFolder As Long
Dim m_fsoObject = New FileSystemObject
If Not m_fsoObject.FolderExists(strCurrentFolder) Then Exit Sub
fsoFolder = m_fsoObject.GetFolder(strCurrentFolder)
On Error Resume Next
'Has sub-folders
If fsoFolder.SubFolders.Count > 0 Then
lngFolder = 1
ReDim strPaths(fsoFolder.SubFolders.Count)
'Get each sub-folders path and add to an array
For Each fsoSubFolder In fsoFolder.SubFolders
strPaths(lngFolder) = fsoSubFolder.Path
lngFolder = lngFolder + 1
Next fsoSubFolder
lngSubFolder = 1
'Recursively call the function for each sub-folder
Do While lngSubFolder < lngFolder
Call DeleteEmptyFolders(strPaths(lngSubFolder))
lngSubFolder = lngSubFolder + 1
Loop
End If
'No sub-folders or files
If fsoFolder.Files.Count = 0 And fsoFolder.SubFolders.Count = 0 Then
fsoFolder.Delete()
End If
End Sub
"Failed to stop service"
The code is as follows:
============================================
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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String
Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\DeleteFolders", True)
strFoldersFromRegistry = MyKey.GetValue("FolderList")
strIntervalFromRegistry = MyKey.GetValue("Interval")
'Split the folder list into an array of individual entries
strFolderList = Split(strFoldersFromRegistry, ";")
'Start the timer and set the interval to the correct number of
milliseconds
Dim dblTimerInterval As Double
dblTimerInterval = CDbl(strIntervalFromRegistry)
Timer1.Interval = (dblTimerInterval * 1000)
Timer1.Start()
EventLog1.WriteEntry("Delete Folders", "DeleteFolders is now
monitoring " & strFoldersFromRegistry)
End Sub
====================================================
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
booRunning = False
'Stop the timer
Timer1.Stop()
EventLog1.WriteEntry("DeleteFolders is no longer monitoring folders")
End Sub
===================================================
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Do While booRunning = True
'Do the "Delete Folders" routine for each folder in the list
Dim strCurrentFolder As String
Dim MyFolder As New FileSystemObject
For Each strCurrentFolder In strFolderList
If Len(strCurrentFolder) = 0 Then Exit Sub
'Check the folder exists
If MyFolder.FolderExists(strCurrentFolder) = True Then
DeleteEmptyFolders(strCurrentFolder)
End If
Next
Loop
End Sub
====================================================
Private Sub DeleteEmptyFolders(ByVal strCurrentFolder As String)
Dim fsoSubFolders As Folders
Dim fsoSubFolder As Folder
Dim fsoFolder As Folder
Dim strPaths()
Dim lngFolder As Long
Dim lngSubFolder As Long
Dim m_fsoObject = New FileSystemObject
If Not m_fsoObject.FolderExists(strCurrentFolder) Then Exit Sub
fsoFolder = m_fsoObject.GetFolder(strCurrentFolder)
On Error Resume Next
'Has sub-folders
If fsoFolder.SubFolders.Count > 0 Then
lngFolder = 1
ReDim strPaths(fsoFolder.SubFolders.Count)
'Get each sub-folders path and add to an array
For Each fsoSubFolder In fsoFolder.SubFolders
strPaths(lngFolder) = fsoSubFolder.Path
lngFolder = lngFolder + 1
Next fsoSubFolder
lngSubFolder = 1
'Recursively call the function for each sub-folder
Do While lngSubFolder < lngFolder
Call DeleteEmptyFolders(strPaths(lngSubFolder))
lngSubFolder = lngSubFolder + 1
Loop
End If
'No sub-folders or files
If fsoFolder.Files.Count = 0 And fsoFolder.SubFolders.Count = 0 Then
fsoFolder.Delete()
End If
End Sub