Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by Audax321
Posted

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... :)

Posted

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... :(

  • *Gurus*
Posted

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".

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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... :)

  • Moderators
Posted

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

Visit...Bassic Software
Guest pumkindrvr
Posted

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

Guest pumkindrvr
Posted

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

Guest pumkindrvr
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...