LOL, ME AGAIN!!!! Ques. Regarding Directory Existance...

Audax321

Well-known member
Joined
May 4, 2002
Messages
90
Okay, I have this code...

Code:
If(system.io.directory.exists(path) = True) THen
    do some crap
End if

Pretty straight forward, right? Its fine when the IF statement is TRUE. If, however, the directory doesnt 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
 
Last edited by a moderator:
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 didnt 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 doesnt it gives me an error if Im running the app from VB or causes the program to hang if Im 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 wouldnt mind posting your code so I can try it in my program would you?? I think my VB is retarded... :)
 
Ok, now Im confused...

I tried making another project and tried a string variable and actual "C:\" text ... and guess what .. it worked!

But, that doesnt 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...

Code:
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 dont understand why it wont 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... :(
 
I dont think its the exists line that causing the error, its more likely because youre deleting items from a listbox while looping forwards through it, which is always a Bad Idea, since youll 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 youre getting, not just "it gives an error".
 
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:

Code:
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... :)
 
In case you want to use a For/next loop ...

Code:
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
 
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
 
Ok,
Im 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, Im new.
P
 
VB.NET is very different to VB6. It appears you gave him a VB6 answer.
 
I see. I thought they were the same. Guess Im in the wrong forum. Sorry, my mistake.

Audax321, Hope that didnt mess you up.
P
 
Back
Top