If / Else nested in a For / Next statement is failing

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Greetings All,
My application creates a folder for flat file storage. During setup or first run by the I.T. department I give them the choice of which logical drive the folder will be created on, so that all clients have access to it.
When a client open the application form I do the following to "look" for the folder. Private Sub NM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Find folder
For Each d As String In Directory.GetLogicalDrives
If My.Computer.FileSystem.DirectoryExists((d) + "QPILABSMachines") Then
Label7.Text = ((d) + "QPILABSMachines")
Else
MsgBox("No QPILABS Machine folder was found. If the folder if on a network drive insure it is on-line and accessible. Contact your Network Administrator for further information.")
Close and give focus to form Main.vb
Me.Close()
End If
Next
End Sub
What happens is once the folder is found, the loop continues and my "Else" fires, even though the folder was found. So I read that I need to exit the For loop. I tried this uber simple fix.
Private Sub NM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each d As String In Directory.GetLogicalDrives

If My.Computer.FileSystem.DirectoryExists((d) + "QPILABSMachines") Then
Label7.Text = ((d) + "QPILABSMachines")

Exit For

Else
MsgBox("No QPILABS Machine folder was found. If the folder if on a network drive insure it is on-line and accessible. Contact you Network Administrator for further information.")
Close and give focus to form Main.vb
Me.Close()
End If
Next
End Sub
The Exit For work if the folder is found on the first listed logical drive, in my case my C drive. If I cut and paste the folder to a different drive the Else statement will run.
I need a better way to exit the loop me thinks. another nested IF Then?
Thoughts?

View the full article
 
Back
Top