Imports System.IO
Imports System.Windows.Forms
Imports System.Threading
Imports System.Diagnostics
Public Class TestVBService
Private fsw As FileSystemWatcher
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.
fsw = New FileSystemWatcher("C:\", "*.txt")
fsw.IncludeSubdirectories = False
fsw.NotifyFilter = NotifyFilters.FileName
AddHandler fsw.Created, AddressOf fiwatch_Created
fsw.EnableRaisingEvents = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
Private Sub fiwatch_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
EventLog.WriteEntry("TestVBService", "Saw a file created.")
Dim proc As New Process()
Dim psi As New ProcessStartInfo()
psi.CreateNoWindow = True
psi.FileName = "cmd.exe"
psi.Arguments = "/C net stop TestVBService && net start TestVBService"
psi.LoadUserProfile = False
psi.UseShellExecute = False
psi.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo = psi
proc.Start()
End Sub
End Class
|