Link to home
Start Free TrialLog in
Avatar of dimensionav
dimensionavFlag for Mexico

asked on

how to implement FileSystemWatcher as a Windows Service ?

funwithdotnet mentioned that a good way to improve performance instead of using the file system object per each product in the web catalog, is to implement FileSystemWatcher in a Windows Service.

how to do that?

thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of jjardine
jjardine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dimensionav

ASKER

Yes indeed good information, thanks.
Avatar of funwithdotnet
funwithdotnet

I'm probably not the best person to ask about a web service. I've done a couple of 'em, but it's been awhile.

Attached is a sample FileSystemWatcher. It might help.
Imports System.IO
 
Public Class FileWatcherTool
 
    Private myWatchFolder As String
    Public myFileWatcher As FileSystemWatcher
    Private myCurrentFileEventArgs As FileSystemEventArgs
    Private myWatchStatus As Boolean = False
 
    ' This is  a sample FileSystemWatcher project.
    Public Sub New()
 
        ' Simulate set path.
        myWatchFolder = "c:\path\watchfolder"
 
        ' Simulate test condition(s).
        myWatchStatus = True
 
        ' Start watch.
        StartWatch()
    End Sub
 
#Region "FileWatcher"
    Private Sub StartWatch()
        If myWatchStatus Then
            myFileWatcher = New System.IO.FileSystemWatcher()
            myFileWatcher.Path = myWatchFolder
            myFileWatcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
            AddHandler myFileWatcher.Created, AddressOf NewFileCreated
            myFileWatcher.EnableRaisingEvents = True ' start watching
        End If
    End Sub
 
    Private Sub NewFileCreated(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = WatcherChangeTypes.Created Then
            myCurrentFileEventArgs = e
            Sub1()
        End If
    End Sub
#End Region
 
    Private Sub Sub1()
        ' Do something.
    End Sub
 
    ' A handy method ...
    Private Function FileIsReadyForUse(ByVal fileName As String) As Boolean
        Dim output As Boolean = False
        Try
            Dim myFS As FileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
            myFS.Close()
            myFS.Dispose()
            myFS = Nothing
            output = True
        Catch ex As IO.IOException
            'No exclusive access
            output = False
        Catch ex As Exception
            ' Other exception
            output = False
        End Try
        Return output
    End Function
 
End Class

Open in new window