2

I sometimes get false negatives when I check if a directory on a network path exists.

When I check if the folder exists the method returns 'true' most of the time. Sometimes it switches to 'false' and reports 'false' for every call for about 5 seconds.
I think it changes to 'false' when someone who hasn't accessed the network drive for a while (maybe 15 minutes) accesses it for the first time. The network drive is not offline during these 5 seconds. I'm still able to navigate it in Windows Explorer. Therefore I am not sure that this really causes the false negatives.

Even the Directory.CreateDirectory() method fails sometimes with an error stating the directory already exists. But the docs say it only tries to create the directory if it does not already exist.

It doesn't matter whether I use

  • new DirectoryInfo("PATH").Exists;
  • Directory.Exists("PATH");
  • Upon request in the comments I also tried
    var di = new DirectoryInfo("PATH");
    di.Refresh();
    di.Exists;

And which format for the path I use

  • N:\MyFolder (N: is the same previousely mapped network drive)
  • \\192.168.1.10\MyShare\MyFolder

I saved the credentials to this server in Windows Credentials Manager.

Does anyone have any idea what could cause this inconsistency?

renklus
  • 776
  • 6
  • 17
  • Are you completely certain that the user running the program has permissions to (at least) read that directory? – Camilo Terevinto Jan 12 '18 at 11:25
  • @CamiloTerevinto I assumed the user has permissions because the check returns 'true' most of the time. Also the only time I entered the password was in Windows Credentials Manager. The mapped network drive and the UNC path both have to use this credential. For testing I just start the .exe myself. I think this means it runs as my user (and therefore uses the credentials from my Credentials Manager). – renklus Jan 12 '18 at 11:31
  • You need to use `DirectoryInfo.Refresh`. This is due to how `FileSystemInfo` (base class of directoryinfo) behaves. – Syntax Error Jan 12 '18 at 11:54
  • 1
    You need to retrieve exact system error code for failed operation to get what's going on (via Marshal.GetLastWin32Error), because Directory.Exists doesn't check for actual existence, it checks was directory available at the moment or not. – arbiter Jan 12 '18 at 11:57
  • @MrJF Thanks. Unfortunately this made no difference. – renklus Jan 12 '18 at 15:47
  • Does this -https://stackoverflow.com/questions/8331467/directoryinfo-exists-always-returns-false-during-mstest/8331673#8331673 - help point you in the right direction? I've had similar issues before and calling a refresh to remove the cached `directoryinfo` value helped. – Syntax Error Jan 12 '18 at 15:57
  • @arbiter Thank you. Just after Directory.Exists() returns 'false' the first time Win32Error is still 0. And after Directory.CreateDirectory() throws the exception I get the Win32Error 183 (ERROR_ALREADY_EXISTS: Cannot create a file when that file already exists.) – renklus Jan 12 '18 at 15:58
  • @MrJF This question seems to be about refreshing an old DirectoryInfo object. I haven't written to the directory for a while and I create a new DirectoryInfo before every use. – renklus Jan 12 '18 at 16:20
  • The error already exists can happen if you try to create a directory that has the same name as a existing file in the folder. Check your code elsewhere to see if you are accidentally creating a file named `MyFolder` instead of a directory. – Scott Chamberlain Jan 12 '18 at 19:38

0 Answers0