title: System.IO.IOException: Cannot create a file when that file already exists. Even after deleting the existing file
body: <p>Using .NET Core 3.1 and C#, I'm trying to move a directory (including all subdirectories and files) to another directory. The destination directory may contain folders and files that already exist with the same name as the source directory, for example &quot;source/folder/file.txt&quot; may already exist in &quot;destination/folder/file.txt&quot; but I would like to overwrite everything in the destination directory.</p>
  <p>The error I am getting is &quot;System.IO.IOException: Cannot create a file when that file already exists.&quot;, however I am deleting the file that already exists in the destination before moving the file from the source (File.Delete before File.Move), so I don't understand why I am getting this error. Also to add, I am not able to reproduce this error 100% of the time for some reason.</p>
  <p>This is the code I am using to move a directory (lines 137 - 155):</p>
  <pre><code>        public static void MoveDirectory(string source, string target)
          {
              var sourcePath = source.TrimEnd('\\', ' ');
              var targetPath = target.TrimEnd('\\', ' ');
              var files = Directory.EnumerateFiles(sourcePath, &quot;*&quot;, SearchOption.AllDirectories)
                                   .GroupBy(s =&gt; Path.GetDirectoryName(s));
              foreach (var folder in files)
              {
                  var targetFolder = folder.Key.Replace(sourcePath, targetPath);
                  Directory.CreateDirectory(targetFolder);
                  foreach (var file in folder)
                  {
                      var targetFile = Path.Combine(targetFolder, Path.GetFileName(file));
                      if (File.Exists(targetFile)) File.Delete(targetFile);
                      File.Move(file, targetFile);
                  }
              }
              Directory.Delete(source, true);
          }
  </code></pre>
  <p>This is the stack trace of my error:</p>
  <pre><code>Description: The process was terminated due to an unhandled exception.
  Exception Info: System.IO.IOException: Cannot create a file when that file already exists.
     at System.IO.FileSystem.MoveFile(String sourceFullPath, String destFullPath, Boolean overwrite)
     at Module_Installer.Classes.Bitbucket.MoveDirectory(String source, String target) in F:\git\module-installer\module-installer\Module Installer\Classes\Bitbucket.cs:line 147
     at Module_Installer.Classes.Bitbucket.DownloadModuleFiles(Module module, String username, String password, String workspace, String repository, String commitHash, String versionNumber, String downloadDirectory, String installDirectory) in F:\git\module-installer\module-installer\Module Installer\Classes\Bitbucket.cs:line 113
     at Module_Installer.Classes.OvernightInstall.ProcessInstalledModule(TenantModule tenantModule, Boolean skipBackup) in F:\git\module-installer\module-installer\Module Installer\Classes\OvernightInstall.cs:line 393
     at Module_Installer.Classes.OvernightInstall.Run(Boolean skipBackup) in F:\git\module-installer\module-installer\Module Installer\Classes\OvernightInstall.cs:line 75
     at Module_Installer.Program.Main(String[] args) in F:\git\module-installer\module-installer\Module Installer\Program.cs:line 40
  </code></pre>
  <p>This error is happening when I am running the application via Windows Task Scheduler, which I have set to run at 03:30am every day, I have specified that the task should &quot;Start In&quot; the same folder as where the EXE is located.</p>
  <p>Any suggestions would be appreciated, thanks!</p>