سؤال

I have an error handler in place so that when the network connection drops, it keeps checking it the connection has been restored and then starts FileSystemWatcher again. To do this, I currently run this code:

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

I was hoping that the Do Until... Loop would work to check if the connection is working again, however this does not seem to work and so it does not continue through the rest of the code (the first tooltip shows, so I know it is not an issue with my error handler)

It may not be the best way to check for a restored connection, but I don't really see why it wouldn't work. If it definitely won't, could someone explain why or if it should, is there something I have missed here? Please also note, I am fairly new to VB.Net :)

Also, should you require it, checkItem is running the following procedure (plus some logging that is not displayed here)

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
هل كانت مفيدة؟

المحلول

Found the problem. Missing a "\" form the end of my path in the Dir section. It now reads

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

which works fine

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top