我需要监视根文件夹,在该文件夹下有多个文件夹,每个文件夹包含多个文件(.csv)这些文件正在使用其他第三方工具从服务器复制到工作站。我必须监视工作站根目录的文件夹,并且当在任何子文件夹中创建或更新.csv文件时,我必须将.csv文件数据(更新的或新的)写入sql server数据库。在将数据写入sql server时,我需要捕获更新/创建.csv文件的子文件夹名称。这样我就可以在一个数据库文件中包含单独的文件夹数据。
在工作站pc上运行程序时,应用程序崩溃而没有任何特定的错误消息。由于应用程序是在远程PC上运行的,我无法调试应用程序以检查导致崩溃的行。以下是代码:
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
结束班
任何帮助都将不胜感激。
答案 0 :(得分:0)