filesystemwatcher service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joel

    #1

    filesystemwatcher service

    I created a service to watch the c: drive for the creation of exe's and write an event. The problem is anytime I create several vbs files as a test and then delete them, explorer.exe crashes. I think it has something to do with the fact that new files are created in the recycle bin upon deletion. Here is my code

    Imports System.ServiceP roces
    Imports System.I

    Public Class FSWatche
    Inherits System.ServiceP rocess.ServiceB as

    ' Path for the file wathcer to watch. The D: drive is the FTP server driv
    Private Path As String = "c:\

    Protected Overrides Sub OnStart(ByVal args() As String
    ' Add code here to start your service. This method should set thing
    ' in motion so your service can do its work
    ' Tr
    Dim watcher As New FileSystemWatch e
    watcher.Path = Pat
    watcher.Include Subdirectories = Tru
    'watcher.Intern alBufferSize = 819
    watcher.NotifyF ilter = (NotifyFilters. LastAccess Or
    NotifyFilters.L astWrite Or
    NotifyFilters.F ileName

    watcher.Filter = "*.exe

    ' Add event handlers

    AddHandler watcher.Created , AddressOf OnChange

    ' Begin watching
    watcher.EnableR aisingEvents = Tru

    End Su

    Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEvent Args
    ' Specify what is done when a file is changed, created, or deleted
    'Select Case Tru
    ' Case LCase(Left(e.Fu llPath, 11) = "c:\recycle d"
    ' Exit Su
    ' Case LCase(Right(e.F ullPath, 3) = "exe"
    ' Exit Selec
    ' Case LCase(Right(e.F ullPath, 3) = "vbs"
    ' Exit Selec
    ' Case Els
    ' Exit Su
    'End Selec

    Dim changeTyp
    Dim shostname As Strin
    If e.ChangeType = WatcherChangeTy pes.Created The
    changeType = "created
    End I

    shostname = System.Net.Dns. GetHostNam

    'Console.WriteL ine(shostname & ": file '" & e.FullPath & "' was " & changeType
    Dim MyLog As New EventLog ' create a new event log

    If Not MyLog.SourceExi sts("FSWatcher2 ") The
    MyLog.CreateEve ntSource("FSWat cher2", "FSWatcher2 Log") ' Create Log
    End I
    MyLog.Source = "FSWatcher2 Log
    ' Write to the Log
    MyLog.WriteEntr y("FSWatcher2 Log", "FSWatcher2 alert: " &
    CStr(TimeOfDay) & vbCrLf & shostname & " - file '" &
    e.FullPath & "' was " & changeType

    End Su
    Protected Overrides Sub OnStop(
    ' Add code here to perform any tear-down necessary to stop your service
    End Su

    End Clas

Working...