How To: Prevent Timeout When Inspecting Unavailable Network Share - C#

前端 未结 4 1914
别那么骄傲
别那么骄傲 2020-12-14 03:25

We have some basic C# logic that iterates over a directory and returns the folders and files within. When run against a network share (\\\\server\\share\\folder) that is ina

相关标签:
4条回答
  • 2020-12-14 03:41

    See...

    Faster DirectoryExists function?

    ...for a way of setting the execution time for Directory.Exists

    0 讨论(0)
  • 2020-12-14 03:43

    Perhaps you could try pinging the server first, and only ask for the directory info if you get a response?

    0 讨论(0)
  • 2020-12-14 03:44

    You can use this code:

    var task = new Task<bool>(() => { var fi = new FileInfo(uri.LocalPath); return fi.Exists; });
    task.Start();
    
    return task.Wait(100) && task.Result;
    
    0 讨论(0)
  • 2020-12-14 03:46

    Place it on its own thread, if it doesn't come back in a certain amount of time, move on.

    0 讨论(0)
提交回复
热议问题