FileSystemWatcher
-
I am trying to watch for "n" number of folders. Do I have to create "n" number of FileSystemWatcher instance to do this? If anybody worked on this please advice. Thanks a lot. :rolleyes: Zulfikar Ali
-
I am trying to watch for "n" number of folders. Do I have to create "n" number of FileSystemWatcher instance to do this? If anybody worked on this please advice. Thanks a lot. :rolleyes: Zulfikar Ali
Is more easy create one folder and five subfolders. You only watch for the parent folder or root folder and FilSystemWatcher, with "true" in watch subdirectories option, watch the six folders. Victor
-
Is more easy create one folder and five subfolders. You only watch for the parent folder or root folder and FilSystemWatcher, with "true" in watch subdirectories option, watch the six folders. Victor
Thanks for your reply. But folders can't be sub folder in my project. Anyway i have figured out the solution. Here is the code if anybody is interested. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer Dim strName As String Dim strDBReject As String Dim strParseReject As String 'Load the profile info from the setup XML file Dim dsGetIni As New DataSet() dsGetIni.ReadXml(Application.StartupPath & "\setup.xml") Dim dtGetIni As DataTable = dsGetIni.Tables("Profile") i = dtGetIni.Rows.Count Dim objRow As DataRow() For i = 0 To dsGetIni.Tables("Profile").Rows.Count - 1 strName = dtGetIni.Rows(i)("Name") strDBReject = dtGetIni.Rows(i)("DBReject") watcher(strDBReject) strParseReject = dtGetIni.Rows(i)("ParseReject") watcher(strParseReject) Next End Sub Public Function watcher(ByVal strFolder As String) Dim objWatcher As New System.IO.FileSystemWatcher() 'Folder to watch objWatcher.Path = strFolder 'File extension to watch objWatcher.Filter = "*.pdf" 'Create delegates to handle the events for the FileSystemWatcher AddHandler objWatcher.Created, AddressOf OnFileCreated 'Exclude subdirectories from watching objWatcher.IncludeSubdirectories = True 'Start watching folder objWatcher.EnableRaisingEvents = True End Function Public Sub OnFileCreated(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) Try 'create new instance of importer Dim FileInfo As New FileInformation(e.FullPath) ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ProcessFile), FileInfo) Catch ex As Exception End Try End Sub Private Function GetExclusiveAccess(ByVal FilePath As String) As Boolean Try Dim theFile As File Dim strm As Stream = theFile.Open(FilePath, FileMode.Open) 'if we succeed, we can let it go strm.Close() GetExclusiveAccess = True Catch e As Exception GetExclusiveAccess = False End Try End Function Public Sub ProcessFile(ByVal fileInfo As Object) If Thread.CurrentThread.Name = "" Then Thread.CurrentThread.Name = Thread.CurrentThread.GetHa