2014-02-17 106 views
0

我有一個錯誤處理程序,以便當網絡連接斷開時,它會不斷檢查連接已恢復,然後再次啓動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將努力檢查連接重新工作,但是這似乎並沒有工作,所以它不會通過休息繼續的代碼(第一個工具提示顯示,所以我知道這不是我的錯誤處理程序的問題)

它可能不是檢查恢復連接的最佳方法,但我真的不明白爲什麼它wouldn沒有工作。如果肯定不會,有人可以解釋爲什麼或者應該如何,我在這裏錯過了什麼?另請注意,我是相當新的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

找到了問題。在Dir部分中缺少「\」,表示我的路徑已結束。現在讀

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

工作正常