Speed up File.Exists for non existing network shares

后端 未结 6 439
日久生厌
日久生厌 2020-12-08 19:51

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 curren

相关标签:
6条回答
  • 2020-12-08 19:59

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

    0 讨论(0)
  • 2020-12-08 20:00

    In a nutshell

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

    Edit regarding Bill's comment

    if Google is not the referer, EE doesn't show the answer for free. Links to EE are not helpful.

    OP found the article I've mentioned in my original answer and was kind enough to include the source code for the solution to his question.

    0 讨论(0)
  • 2020-12-08 20:00

    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;

    0 讨论(0)
  • 2020-12-08 20:05

    I found the pathExists with thread timeout function useful but finally realized that it needed a change from File.Exists to Directory.Exists to work correctly.

    0 讨论(0)
  • 2020-12-08 20:06

    Another "thread solution":

    /// <sumary>Check if file exists with timeout</sumary>
    /// <param name="fileInfo">source</param>
    /// <param name="millisecondsTimeout">The number of milliseconds to wait,
    ///  or <see cref="System.Threading.Timeout.Infinite"/> (-1) to wait indefinitely.</param>
    /// <returns>Gets a value indicating whether a file exists.</returns>
    public static bool Exists(this FileInfo fileInfo, int millisecondsTimeout)
    {
        var task = new Task<bool>(() => fileInfo.Exists);
        task.Start();
        return task.Wait(millisecondsTimeout) && task.Result;
    }
    

    Source: http://www.jonathanantoine.com/2011/08/18/faster-file-exists/

    There are some concerns about "drive not responding fast enough", so this is compromise between speed and "the truth". Don't you use It if you want to be sure 100%.

    0 讨论(0)
  • 2020-12-08 20:17

    This worked GREAT for me!
    Here's IsDriveReady() in C#:

    using System.Net;
    private bool IsDriveReady(string serverName)
    {
       // ***  SET YOUR TIMEOUT HERE  ***     
       int timeout = 5;    // 5 seconds 
       System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
       System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
       options.DontFragment = true;      
       // Enter a valid ip address     
       string ipAddressOrHostName = serverName;
       string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
       byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
       System.Net.NetworkInformation.PingReply reply = pingSender.Send(ipAddressOrHostName, timeout, buffer, options);
       return (reply.Status == System.Net.NetworkInformation.IPStatus.Success);
    }
    
    0 讨论(0)
提交回复
热议问题