处理FileSystemWatcher上的连接丢失

时间:2014-02-17 12:14:20

标签: vb.net filesystemwatcher

我有一个错误处理程序,以便在网络连接断开时,它会不断检查连接是否已恢复,然后再次启动FileSystemWatcher。为此,我目前运行此代码:

Sub errhandler(ByVal source As Object, ByVal e As  _
                    System.IO.ErrorEventArgs)

    Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Error
    Me.NotifyIcon1.BalloonTipText = "Connection to the folder has been lost"
    Me.NotifyIcon1.BalloonTipTitle = "Connection Lost"
    Me.NotifyIcon1.ShowBalloonTip(6000)

    Do Until Dir("\\My\Path", vbDirectory) <> vbNullString
        Application.DoEvents()
    Loop
    watchFolder = Nothing
    Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
    Me.NotifyIcon1.BalloonTipText = "Attempting Reconnection"
    Me.NotifyIcon1.BalloonTipTitle = "Attempting Reconnection to folder"
    Me.NotifyIcon1.ShowBalloonTip(6000)
    checkItem()
End Sub

我希望Do Until... Loop能够检查连接是否再次正常工作,但是这似乎不起作用,所以它不会继续通过其余的代码(第一个工具提示显示,所以我知道这不是我的错误处理程序的问题)

这可能不是检查已恢复连接的最佳方法,但我不明白为什么它不起作用。如果它绝对不会,有人可以解释为什么或是否应该,有什么我错过了吗?还请注意,我对VB.Net相当新:)

此外,如果您需要它,checkItem正在运行以下过程(以及此处未显示的一些日志记录)

watchFolder = New System.IO.FileSystemWatcher()
watchFolder.Path = "\\My\Path\"
watchFolder.IncludeSubdirectories = True
watchFolder.EnableRaisingEvents = True
watchFolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchFolder.NotifyFilter = watchFolder.NotifyFilter Or IO.NotifyFilters.FileName
watchFolder.NotifyFilter = watchFolder.NotifyFilter Or IO.NotifyFilters.Attributes
AddHandler watchFolder.Changed, AddressOf logchange
AddHandler watchFolder.Created, AddressOf logchange
AddHandler watchFolder.Deleted, AddressOf logchange
AddHandler watchFolder.Error, AddressOf errhandler
AddHandler watchFolder.Renamed, AddressOf logrename

1 个答案:

答案 0 :(得分:1)

发现问题。缺少“\”形成Dir部分中路径的末尾。它现在读取

Do Until Dir("\\My\Path\", vbDirectory) <> vbNullString
    Application.DoEvents()
Loop

工作正常