[英]VB.Net FileSystemWatcher - Multiple Folders and Multiple Files
I have a requirement of monitoring a root folder under which there are multiple folders and each folder containing multiple files (.csv) These files are being copied to a workstation from a server using some other third party tool. 我需要监视一个根文件夹,该根文件夹下有多个文件夹,每个文件夹包含多个文件(.csv)。这些文件正在使用其他一些第三方工具从服务器复制到工作站。 I have to monitor the workstation root's folder and as an when a .csv file is created or updated in any of the child folders, I have to write the .csv file data (updated or new) to a sql server database.
我必须监视工作站根目录的文件夹,并且在任何子文件夹中创建或更新.csv文件时,都必须将.csv文件数据(更新的或新的)写入sql server数据库。 While writing the data to the sql server, I need to capture the child folder name in which the .csv file was updated/created.
将数据写入sql服务器时,我需要捕获在其中更新/创建.csv文件的子文件夹名称。 This way I can have separate folder's data in one database file.
这样,我可以在一个数据库文件中拥有单独的文件夹数据。
While running the program on the workstation pc, the application crashes without any specific error message. 在工作站PC上运行程序时,应用程序崩溃,没有任何特定的错误消息。 And since the app is run on a remote pc, I cannot debug the app to check which line is causing the crash.
并且由于该应用程序在远程PC上运行,因此我无法调试该应用程序以检查导致崩溃的行。 Below is the code:
下面是代码:
Public Class FolderWatchAndProcess
Dim blnMonitorUpdated As Boolean = True
Dim blnMonitorCreated As Boolean = True
Dim blnShowMessage As Boolean
Dim strFullFileName As String
Dim strFileName As String
Dim strFolderName As String
Private Delegate Sub updateLabel(ByVal newLabel As String)
Private Sub updateLabelHandler(ByVal labelText As String)
lblMetNo.Text = labelText
End Sub
Private Sub ReadAndInsertCSVData()
' This routine checks for duplicates and inserts new records into the sql server database
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'lblMonitorPath.Text = "C:\RootFolder\"
'btnStartWatching_Click(Nothing, Nothing)
'Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub btnStartWatching_Click(sender As Object, e As EventArgs) Handles btnStartWatching.Click
btnStartWatching.Text = "Stop Watching"
Dim aFileWatcherInstance As ArrayList = New ArrayList
For Each sMonitorFolder As String In lstFolders.Items
If IO.Directory.Exists(sMonitorFolder) Then
Dim oFileWatcher As FileSystemWatcher = New FileSystemWatcher
oFileWatcher.Path = sMonitorFolder
oFileWatcher.Filter = "*.CSV"
If blnMonitorUpdated Then
AddHandler oFileWatcher.Changed, AddressOf Me.FileSystemWatcherUpdated
Else
'RemoveHandler , AddressOf Me.FileSystemWatcherUpdated
End If
If blnMonitorCreated Then
AddHandler oFileWatcher.Created, AddressOf Me.FileSystemWatcherCreated
Else
'RemoveHandler , AddressOf Me.FileSystemWatcherCreated
End If
oFileWatcher.EnableRaisingEvents = True
aFileWatcherInstance.Add(oFileWatcher)
End If
Next
End Sub
Private Sub FileSystemWatcherCreated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
strFullFileName = e.FullPath
strFileName = Mid(e.Name, 3, Len(e.Name))
strFolderName = strFileName.Split("_")(0)
If Me.InvokeRequired Then
Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.BeginInvoke(d, New Object() {strFolderName})
Else
updateLabelHandler(strFolderName)
End If
ReadAndInsertCSVData()
End Sub
Private Sub FileSystemWatcherDeleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
'A file has been deleted from the child directory.
End Sub
Private Sub FileSystemWatcherUpdated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
strFullFileName = e.FullPath
strFileName = Mid(e.Name, 3, Len(e.Name))
strFolderName = strFileName.Split("_")(0)
If Me.InvokeRequired Then
Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.BeginInvoke(d, New Object() {strFolderName})
Else
updateLabelHandler(strFolderName)
End If
ReadAndInsertCSVData()
End Sub
End Class 末级
Any help would be appreciated. 任何帮助,将不胜感激。
the Changed event will fire multiple times: Changed事件将触发多次:
https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.io.filesystemwatcher.changed(v=vs.110).aspx
probably you can use the IsFileLocked function that mention at here: 可能您可以使用此处提到的IsFileLocked函数:
I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it? 有时我在System.Threading.Tasks.TaskCompletionSource <bool>上不断出现异常,我该如何解决?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.