Guest User

Untitled

a guest
Aug 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. Using Directory.Exists on a network folder when the network is down
  2. bool pathExists = Directory.Exists(path);
  3.  
  4. Func<bool> func = () => Directory.Exists(path);
  5. Task<bool> task = new Task<bool>(func);
  6. task.Start();
  7. if (task.Wait(100)) {
  8. return task.Value;
  9. } else {
  10. // Didn't get an answer back in time be pessimistic and assume it didn't exist
  11. return false;
  12. }
  13.  
  14. public static bool DirectoryExists(string path)
  15. {
  16.  
  17. try
  18. {
  19. if(System.IO.Directory.GetDirectories(path).Length>0)
  20. return true;
  21. }
  22. catch(Exception exp)
  23. {
  24. }
  25. return false; }
  26.  
  27. [DllImport("WININET", CharSet = CharSet.Auto)]
  28. static extern bool InternetGetConnectedState(ref int lpdwFlags, int dwReserved);
  29.  
  30. public static bool Connected
  31. {
  32. get
  33. {
  34. int flags = 0;
  35. return InternetGetConnectedState(ref flags, 0);
  36. }
  37. }
  38.  
  39. public static bool FolderExists(string directory)
  40. {
  41. if (new Uri(directory, UriKind.Absolute).IsUnc && !Connected)
  42. return false;
  43. return System.IO.Directory.Exists(directory);
  44. }
Add Comment
Please, Sign In to add comment