Home > Mobile >  System.IO.IOException: Cannot create a file when that file already exists. Even after deleting the e
System.IO.IOException: Cannot create a file when that file already exists. Even after deleting the e

Time:12-08

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 "source/folder/file.txt" may already exist in "destination/folder/file.txt" but I would like to overwrite everything in the destination directory.

The error I am getting is "System.IO.IOException: Cannot create a file when that file already exists.", 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.

This is the code I am using to move a directory (lines 137 - 155):

        public static void MoveDirectory(string source, string target)
        {
            var sourcePath = source.TrimEnd('\\', ' ');
            var targetPath = target.TrimEnd('\\', ' ');
            var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
                                 .GroupBy(s => 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);
        }

This is the stack trace of my error:

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

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 "Start In" the same folder as where the EXE is located.

Any suggestions would be appreciated, thanks!

CodePudding user response:

Instead of deleting existing files in the target directory try to overwrite them using File.Move(file, targetFile, overwrite: true).

By the way there is an MSDN example on how to copy directories. It's not exactly your use case, but could be helpful anyway.

  • Related