Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding a Service and FileSystemWatcher - Ideas? Help?

Status
Not open for further replies.

croag

MIS
Nov 8, 2001
49
US
Hello all,


I've been working on an example from vb-helper.com and trying to modify it to be able to apply to a service. Basically, this is going to monitor a directory (using the FSW), then when a new PXP file enters, it's gonna take the info out of the file, and put it into another file. Phew! But right now, I'd like for this to just delete it from the named directory...Please o please help I am up to my ears! My code is posted below. Thanks very much in advance!!! I love reading this forum!

Imports System.ServiceProcess
Imports System.IO
Public Class svcPxpParser
Inherits System.ServiceProcess.ServiceBase



Private strWatchDir As String
Private WithEvents fswDir1 As FileSystemWatcher


Private Overloads Sub OnStart(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Handles MyBase.Load lstFiles.Items.Add(Now.ToString() & " Starting")


' Get the path to the directory we will watch
strWatchDir = "C:\Documents and Settings\Administrator\Desktop\D\Provshare\Remsas\train\IDS\"
'strWatchDir = strWatchDir.Substring(0, strWatchDir.LastIndexOf("\"))
'strWatchDir &= "\Files"

' Make the FileSystemWatcher.
fswDir1 = New FileSystemWatcher
fswDir1.Path = strDirWatch
fswDir1.NotifyFilter = 0
fswDir1.NotifyFilter = fswDir1.NotifyFilter Or NotifyFilters.FileName Or NotifyFilters.Size
fswDir1.EnableRaisingEvents = True

' Process any files that already exist.
ProcessExistingFiles(strWatchDir)
End Sub
' Process existing files.
Private Sub ProcessExistingFiles(ByVal directory_name As String)
Dim dir_info As New DirectoryInfo(directory_name)
Dim file_infos As FileInfo() = dir_info.GetFiles()
For Each fi As FileInfo In file_infos
ProcessFile(fi.FullName)
Next fi
End Sub
' Process a file.
Private Sub ProcessFile(ByVal file_name As String)
lstFiles.Items.Add(Now.ToString() & " Processed " & file_name)
File.Delete(file_name)
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub

End Class
 
I'm still wondering about this one. Is what I'm doing unheard of? Using a service to monitor certain directories? Please me know. Thanks!
 
I'm still in need of some assistance here. Anyone have a bright idea?
Thanks very much!
 
You should minimize the amount of work done in the OnStart event -- try and keep it under 10 seconds. This event is actually running in a thread belonging to the Service Control Manager, not you.

If you have a long running process, then you should start your own thread to do the work (see System.Threading namespace).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top