Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to implement FileSystemWatcher as a Windows Service ?

Posted on 2009-03-30
3
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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