Solved

how to implement FileSystemWatcher as a Windows Service ?

Posted on 2009-03-30
3
367 Views
Last Modified: 2013-11-07
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.
0
Comment
Question by:dimensionav
3 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 500 total points
ID: 24025311
this site gives you a tutorial for creating a windows service.  That would be a good place to start.  http://www.codeguru.com/vb/gen/vb_system/services/article.php/c4825
0
 

Author Closing Comment

by:dimensionav
ID: 31564655
Yes indeed good information, thanks.
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 24045669
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Help with retrieving partial value from a column using VB.NET 4 39
Problem to error 4 43
ASP.net Run 3 sliders vertically as opposed to horizontally 9 18
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now