Audax321 Posted November 1, 2002 Posted November 1, 2002 (edited) Okay, I have this code... If(system.io.directory.exists(path) = True) THen 'do some crap End if Pretty straight forward, right? It's fine when the IF statement is TRUE. If, however, the directory doesn't exist, instead of just quietly skipping the code, it decides to give me an error and crash like a spoiled brat.... Anyone know, how to fix this? Thanks.. :D Edited November 1, 2002 by Audax321 Quote
Moderators Robby Posted November 1, 2002 Moderators Posted November 1, 2002 It works for me. Quote Visit...Bassic Software
Audax321 Posted November 1, 2002 Author Posted November 1, 2002 rrrrrrgh.. that is so odd.. I even made a new project with just that code in it to make sure nothing else was conflicting and it still didn't work. Its fine as long as the directory exists. But if I delete the directory and then the program checks to see if it exists when it doesn't it gives me an error if I'm running the app from VB or causes the program to hang if I'm running it from outside VB. I even checked the VB help section .. and they say that it returns either True or False, but its doing something queer in my program. You wouldn't mind posting your code so I can try it in my program would you?? I think my VB is retarded... :) Quote
Audax321 Posted November 1, 2002 Author Posted November 1, 2002 Ok, now I'm confused... I tried making another project and tried a string variable and actual "C:\" text ... and guess what .. it worked! But, that doesn't really help me. All of the paths that I need to check are contained in a listbox.. here is the actual code from my application... If (MenuItem11.Checked = True) Then For inti = 0 To lstSelDir.Items.Count - 1 If (System.IO.Directory.Exists(lstSelDir.Items.Item(inti)) = False) Then lstSelDir.Items.RemoveAt(inti) inti = inti - 1 End If Next inti End If I don't understand why it won't work with the listbox.items.item(inti) displaying the path because it returns the path as a string I believe. Even setting the path I get from the listbox to a string variable and then checking the variable does not help any... :( Quote
*Gurus* divil Posted November 1, 2002 *Gurus* Posted November 1, 2002 I don't think it's the exists line that causing the error, it's more likely because you're deleting items from a listbox while looping forwards through it, which is always a Bad Idea, since you'll be trying to reference elements which no longer exist. Try changing your code so you loop backwards through the listbox instead of forwards, and get rid of your inti decrementor. It really helps if you tell us what error you're getting, not just "it gives an error". Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Audax321 Posted November 1, 2002 Author Posted November 1, 2002 Okay, it turns out the error was caused by another line later on that was running because the program was failing to remove the invalid folder. To fix this.. I changed the type of loop from a FOR loop to a DO WHILE loop: If (MenuItem11.Checked = True) Then inti = 0 Do While inti <= lstSelDir.Items.Count - 1 If (System.IO.Directory.Exists(lstSelDir.Items.Item(inti)) = False) Then lstSelDir.Items.RemoveAt(inti) inti = inti - 1 End If inti = inti + 1 Loop End If This seems to have done the trick... Thanks... :) Quote
Moderators Robby Posted November 2, 2002 Moderators Posted November 2, 2002 In case you want to use a For/next loop ... For nCounter = 0 To lstSelDir.Items.Count - 1 If (System.IO.Directory.Exists(lstSelDir.Items.Item(nCounter)) = False) Then lstSelDir.Items.RemoveAt(nCounter) end if Next Quote Visit...Bassic Software
Guest pumkindrvr Posted November 4, 2002 Posted November 4, 2002 Try this You are missing the "Else" find directory "works" if dir("c:\temp",vbDirectory) <> vbNullString then 'do something Else 'do something else end if P Quote
*Gurus* divil Posted November 4, 2002 *Gurus* Posted November 4, 2002 pumkindrvr, this is VB.NET. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pumkindrvr Posted November 4, 2002 Posted November 4, 2002 Ok, I'm new so pardon my ignorance. What does that mean and how should it affect my answer to the original post. I thought it was a easy question to answer(for me). But as I say, I'm new. P Quote
*Gurus* divil Posted November 4, 2002 *Gurus* Posted November 4, 2002 VB.NET is very different to VB6. It appears you gave him a VB6 answer. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pumkindrvr Posted November 4, 2002 Posted November 4, 2002 I see. I thought they were the same. Guess I'm in the wrong forum. Sorry, my mistake. Audax321, Hope that didn't mess you up. P Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.