Speed up File.Exists for non existing network shares

后端 未结 6 442
日久生厌
日久生厌 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 20:06

    Another "thread solution":

    /// Check if file exists with timeout
    /// source
    /// The number of milliseconds to wait,
    ///  or  (-1) to wait indefinitely.
    /// Gets a value indicating whether a file exists.
    public static bool Exists(this FileInfo fileInfo, int millisecondsTimeout)
    {
        var task = new Task(() => 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%.

提交回复
热议问题