views:

1207

answers:

4

I have to check if a set of file paths represent an existing file.

It works fine except when the path contains a network share on a machine that's not on the current network. In this case it takes a pretty long time (30 or 60 seconds) to timeout.

Questions

  • Is there a way to shorten the timeout for non existing network shares? (I'm certain that when they do exist they'll answer quickly, so a timeout of 1 sec would be fine)

  • Is there any other way to solve this issue without starting to cache and making the algorithm more complex? (ie, I already know these X network shares don't exist, skip the rest of the matching paths)

UPDATE: Using Threads work, not particularly elegant, though

public bool pathExists(string path) 
{
    bool exists = true;
    Thread t = new Thread(
                          new ThreadStart(delegate () 
                                          {
                                            exists = System.IO.File.Exists(path); 
                                          })
                         );
    t.Start();
    bool completed = t.Join(500); //half a sec of timeout
    if (!completed) { exists = false; t.Abort(); }
    return exists;
}

This solution avoids the need for a thread per attempt, first check which drives are reachable and store that somewhere.

Experts exchange solution:

8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--

First of all, there is a "timeout" value that you can set in the IsDriveReady function. I have it set for 5 seconds, but set it for whatever works for you.

3 methods are used below: 1. The first is the WNetGetConnection API function that gets the UNC (\servername\share) of the drive 2. The second is our main method: The Button1_Click event 3. The third is the IsDriveReady function that pings the server.

This worked great for me! Here you go:

'This API Function will be used to get the UNC of the drive
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias _
"WNetGetConnectionA" _
(ByVal lpszLocalName As String, _
ByVal lpszRemoteName As String, _
ByRef cbRemoteName As Int32) As Int32


'This is just a button click event - add code to your appropriate event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim bIsReady As Boolean = False

    For Each dri As IO.DriveInfo In IO.DriveInfo.GetDrives()

        'If the drive is a Network drive only, then ping it to see if it's ready.
        If dri.DriveType = IO.DriveType.Network Then

            'Get the UNC (\\servername\share) for the 
            '    drive letter returned by dri.Name
            Dim UNC As String = Space(100)
            WNetGetConnection(dri.Name.Substring(0, 2), UNC, 100)

            'Presuming the drive is mapped \\servername\share
            '    Parse the servername out of the UNC
            Dim server As String = _
                 UNC.Trim().Substring(2, UNC.Trim().IndexOf("\", 2) - 2)

            'Ping the server to see if it is available
            bIsReady = IsDriveReady(server)

        Else
            bIsReady = dri.IsReady

        End If


        'Only process drives that are ready
        If bIsReady = True Then
            'Process your drive...
            MsgBox(dri.Name & " is ready:  " & bIsReady)

        End If

    Next

    MsgBox("All drives processed")

End Sub


Private Function IsDriveReady(ByVal serverName As String) As Boolean
    Dim bReturnStatus As Boolean = False

    '***  SET YOUR TIMEOUT HERE  ***
    Dim timeout As Integer = 5    '5 seconds


    Dim pingSender As New System.Net.NetworkInformation.Ping()
    Dim options As New System.Net.NetworkInformation.PingOptions()

    options.DontFragment = True

    'Enter a valid ip address
    Dim ipAddressOrHostName As String = serverName
    Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
    Dim reply As System.Net.NetworkInformation.PingReply = _
                pingSender.Send(ipAddressOrHostName, timeout, buffer, options)

    If reply.Status = Net.NetworkInformation.IPStatus.Success Then
        bReturnStatus = True

    End If

    Return bReturnStatus
End Function

8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--

A: 

I don't know if DirectoryInfo.Exists() has logic for this?

Or you can try this approach: http://www.netomatix.com/CheckShare.aspx

MartinHN
CheckShare is not an option, I've checked it yesterday, it is very slow, when share not exists.
arbiter
+5  A: 

In a nutshell

  1. Build a list of available drives.
  2. Try to resolve the driveletter to a UNC name.
  3. Try to ping the drive.

Edit

For all downvoters without giving any reason: the OP did find the article I've mentioned in the unedited answer and has been able to use the solution to his satisfaction. He was even so kind to include the solution in his question. Instead of downvoting this accepted answer, it would be way more constructive to upvote the question.

Lieven
Experts Exchange is not my very favorite site.
quillbreaker
Lieven
See the comments on Nick's answer.
Vinko Vrsalovic
Good answer is a good answer. But yeah, ee sucks.
AngryHacker
If Google is not the referer, EE doesn't show the answer for free. Links to EE are not helpful.
Bill the Lizard
@Bill the Lizard, I didn't know that, thanks for pointing that out.
Lieven
The RefControl <https://addons.mozilla.org/en-US/firefox/addon/953> extension for Firefox can forge the referer to google for that site.
jleedev
+1  A: 

Use Threads to do the checks. I think that threads can be timed out.

Nick
Yes they can!!!
Vinko Vrsalovic
(Will accept either if it's closed as dupe or a better option appears)
Vinko Vrsalovic
I have been pondering on that concept to but it seems way to much overkill to me to fire a thread for each file to check. Your milleage may vary offcourse.
Lieven
@Lieven: It's a lot better than having your app take two orders of magnitude more time to check if a list of files exist.
Vinko Vrsalovic
@Lieven: That said, I don't find the Threads approach particularly elegant. I hadn't checked the sexchange article. Now I have, will test the approach later.
Vinko Vrsalovic
I am mighty curious as to what approach you are refering to on the *sexchange* site ;)
Lieven
You may not be aware of the original domain name: expertsexchange.com Choose where to split the words.
Vinko Vrsalovic
A: 

Couldn't you just use FileMonitor control for this so that an event fires when it gets removed? Then you can set bool to false;

baeltazor
No, the app only runs to check if a list of files in a database exist.
Vinko Vrsalovic