Link to home
Create AccountLog in
Visual Basic.NET

Visual Basic.NET

--

Questions

--

Followers

Top Experts

Avatar of carpbyte
carpbyte

Cancelling a FileInfo.FileExists timeout in VB.NET
I have a loop of code which is basically seeing if a file exists in a designated path where only the drive letter changes but never the rest of the path.

Problem is that there doesn't seem to be an easy way to determine if the path DOESN'T exist, so no processing time is wasted.

So my question is two fold -

First, is there a call I can make thatwill instantly determine if a path exists (without timing out)

and Second

If I add a Cancel button to the process which sets a cancel flag, how can I trap the flag?

(I tried putting in Application.DoEvents, but when its stuck on a directory that doesn't exist, it never catches the flag. I also tried using the Directory object and it too has a long time out..)

                        If fi.Exists = True Then
                            lvFound.Items.Add(fi.FullName)
                        End If

                        Application.DoEvents()
                        If bCancel = True Then
                            bCancel = False
                            MsgBox("Search Canceled", MsgBoxStyle.Exclamation, "Locate Files")
                            butCancel.Enabled = False
                            ssStatus.Text = ""
                            Cursor.Current = Cursors.Default
                            Exit Sub
                        End If

???

Thanks

Carp

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of VBRocksVBRocks🇺🇸

In VS.NET:

        If My.Computer.FileSystem.FileExists(fi.FullName) = True Then
              'Process...
        End If

Avatar of carpbytecarpbyte

ASKER

Nope same problem.

If the directory doesn't exist there's no way to interupt the process until the timeout.

Thanks for the response though.

Example:
"Q" is a mapped drive that does NOT have the  subdirectory \MyFiles\MyImages\:

.FileExists (Q:\MyFIles\MyImages\afile.bmp) 'this will not release until it times out, so the process can't be interrupted....




Avatar of VBRocksVBRocks🇺🇸

That's pretty weird, because when I try running this, it returns immediately:

        'Obviously "somefakepath" isn't a valid path.
        If My.Computer.FileSystem.FileExists("somefakepath") = True Then
              'Process...
        End If

And it doesn't matter what the path is.  I can enter a path with a fake drive letter, but same thing:  immediate return.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of VBRocksVBRocks🇺🇸

Just for kicks, why don't you try this and see if it acts the same:

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(fi.FullName) = True Then
       MsgBox "Exists"

    Else
        MsgBox "Does not exist"
       
    End If

I should have double checked - actually the issue is not when the file path doesn't exist, it's when the mapped drive itself is either restricted or errors.

I should re-phrase the question -

Is there a way to check the connected drive list to see if the connection is actually valid without waiting for the timeout?

This gets me a list of mapped drives, HOWEVER they may not be all accesible -

        dri = DriveInfo.GetDrives

For my particular issue, one of the drives in the array generates a "An Error Occured While reconnecting ..." message even when clicked form Explorer, thus the excessive time out.

So the REAL question is if there is a way to quickly determine if a dirve is valid.

I think the answer is no...

(Thanks again for taking a look and I apologize for incorrectly stating the issue...)

Carp

Avatar of VBRocksVBRocks🇺🇸

Try this and see if this will work for you:

        For Each dri As IO.DriveInfo In IO.DriveInfo.GetDrives()
            If dri.IsReady = True Then
                MsgBox(dri.Name & " is ready")

            End If
        Next

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of VBRocksVBRocks🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Forgot to comment - that's what I was looking for - thanks!

Carp
Visual Basic.NET

Visual Basic.NET

--

Questions

--

Followers

Top Experts

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,