title: Can Directory.Move fail because I am browsing the folder?
body: <p>I have two folder in a remote FileShare and I am trying to move the first one inside the second. To do this, I wrote a CLR that pretty much does the following:</p>
  
  <pre><code>if (Directory.Exists(destinationFolder))
  {
      Directory.Delete(destinationFolder, true);
  }
  
  if (Directory.Exists(sourceFolder))
  {
      Directory.Move(sourceFolder, destinationFolder);
  }
  </code></pre>
  
  <p>This works as expected but there are some cases that am getting the following error: </p>
  
  <blockquote>
    <p>System.IO.IOException: Cannot create a file when that file already exists.
    System.IO.IOException: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalMove(String sourceDirName, String destDirName, Boolean checkHost)</p>
  </blockquote>
  
  <p>I was not able to narrow it down. It seems random to me since I can not reproduced it. I run this code block, over 50 times and I could not get the same error (or any error to tell the truth) as before. 
   - Do you see anything wrong with code?
   - Do you have any "guesses" on what may caused this error?</p>
  
  <p>The only thing I can think, is even though the <code>Directory.Delete(destinationFolder, true);</code> return the system does not delete the directory immediately and thus when <code>Directory.Move(sourceFolder, destinationFolder);</code> runs, the <code>destinationFolder</code> still exists.</p>
  
  <p><strong>(29/12/2016)</strong> This is not a duplicate of <a href="https://stackoverflow.com/questions/12667770/cannot-create-a-file-when-that-file-already-exists-when-using-directory-move">Cannot create a file when that file already exists when using Directory.Move</a>. There, the user has a 'mistake' in her code and creates (Directory.CreateDirectory(destinationdirectory);) the destination folder. I am not creating the destination folder, nevertheless, I am deleting it if exists. I looked the comments and the answers but none of them gave a solution to my issue. </p>
  
  <p><strong>(30/12/2016)</strong> I have tried all the suggestions from the comments and answer but still nothing strange happens. No errors and no unexpected behaviors.</p>