Hi,
I have a windows service that is monitoring a folder for activity such as created, changed, deleted, or renamed files. I was able to code the service to log activity into the Event Viewer, and everything was logged accordingly when I did it this way. But when I code the service to log activity into a text file, only the OnStart Sub will log when the service was started. When I change, create, delete, or rename a file in the folder I set the service to watch, I get nothing. This is what i have...
Imports System.ServiceProcess
Imports System.IO
Imports System.Diagnostics
Public Class WatchFolderService
Inherits System.ServiceProcess.ServiceBase
Public watchfolder As FileSystemWatcher
Dim LogFile As StreamWriter = File.AppendText("C:\Documents and Settings\jnguyen\My Documents\TestLog\Log.txt")
Protected Overrides Sub OnStart(ByVal args() As String)
If args.Length > 0 Then
FolderWatcher.Path = args(0).ToString()
End If
LogFile.WriteLine(DateTime.Now.ToShortDateString() & Chr(32) & DateTime.Now.ToShortTimeString() & Chr(32) & Chr(32) & (String.Format("Watch Folder Service starting. " & "Watch folder path is {1}", args.Length, FolderWatcher.Path)))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FolderWatcher.Created
LogFile.WriteLine(String.Format("File: " & e.FullPath & " has been added"))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FolderWatcher.Changed
LogFile.WriteLine(String.Format("File: " & e.FullPath & " has been changed"))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FolderWatcher.Deleted
LogFile.WriteLine(String.Format("File: " & e.FullPath & " has been deleted"))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FolderWatcher.Renamed
LogFile.WriteLine(DateTime.Now.ToLongDateString(), String.Format("File renamed " & "from {0} to {1}.", e.OldFullPath, e.FullPath))
LogFile.Close()
End Sub
End Class
I have a windows service that is monitoring a folder for activity such as created, changed, deleted, or renamed files. I was able to code the service to log activity into the Event Viewer, and everything was logged accordingly when I did it this way. But when I code the service to log activity into a text file, only the OnStart Sub will log when the service was started. When I change, create, delete, or rename a file in the folder I set the service to watch, I get nothing. This is what i have...
Imports System.ServiceProcess
Imports System.IO
Imports System.Diagnostics
Public Class WatchFolderService
Inherits System.ServiceProcess.ServiceBase
Public watchfolder As FileSystemWatcher
Dim LogFile As StreamWriter = File.AppendText("C:\Documents and Settings\jnguyen\My Documents\TestLog\Log.txt")
Protected Overrides Sub OnStart(ByVal args() As String)
If args.Length > 0 Then
FolderWatcher.Path = args(0).ToString()
End If
LogFile.WriteLine(DateTime.Now.ToShortDateString() & Chr(32) & DateTime.Now.ToShortTimeString() & Chr(32) & Chr(32) & (String.Format("Watch Folder Service starting. " & "Watch folder path is {1}", args.Length, FolderWatcher.Path)))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FolderWatcher.Created
LogFile.WriteLine(String.Format("File: " & e.FullPath & " has been added"))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FolderWatcher.Changed
LogFile.WriteLine(String.Format("File: " & e.FullPath & " has been changed"))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FolderWatcher.Deleted
LogFile.WriteLine(String.Format("File: " & e.FullPath & " has been deleted"))
LogFile.Close()
End Sub
Private Sub FolderWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FolderWatcher.Renamed
LogFile.WriteLine(DateTime.Now.ToLongDateString(), String.Format("File renamed " & "from {0} to {1}.", e.OldFullPath, e.FullPath))
LogFile.Close()
End Sub
End Class