Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create a file when that file already exists when using Directory.Move

Tags:

c#

I am trying to move the directory from one location to another location on the same drive. I am getting "Cannot create a file when that file already exists" error. Below is my code.

could any one suggest on this?

        string sourcedirectory = @"F:\source";         string destinationdirectory = @"F:\destination";          try         {             if (Directory.Exists(sourcedirectory))             {                 if (Directory.Exists(destinationdirectory))                 {                   Directory.Move(sourcedirectory, destinationdirectory);                 }                 else                 {                   Directory.CreateDirectory(destinationdirectory);                   Directory.Move(sourcedirectory, destinationdirectory);                 }             }          }         catch (Exception ex)         {             log(ex.message);         } 
like image 335
user1428019 Avatar asked Oct 01 '12 05:10

user1428019


People also ask

Can not create a file when that file already exists?

Press Windows key + R to open up a Run dialog box. Then, type ms-settings:windowsupdate and press Enter to open up the Windows Update tab inside the Settings app. Run dialog: ms-settings:windowsupdate. Inside the Windows Update screen, click on Check for updates and install every available pending update.

What happens if a file already exists?

The error message "File Already Exists" or "File Already in Use" indicates that a file CTI Navigator Desktop is attempting to use is already open, locked or corrupted. The fix is to replace a corrupted file, and close, unlock or replace a locked file.

Does File move overwrite?

File. Move() doesn't support overwriting of an existing file. In fact, it will throw an IOException if a file with the same path as sourceDestFilename already exists.


1 Answers

As both of the previous answers pointed out, the destination Directory cannot exist. In your code you are creating the Directory if it doesn't exist and then trying to move your directory, the Move Method will create the directory for you. If the Directory already exists you will need to Delete it or Move it.

Something like this:

class Program {     static void Main(string[] args)     {         string sourcedirectory = @"C:\source";         string destinationdirectory = @"C:\destination";         string backupdirectory = @"C:\Backup";         try         {             if (Directory.Exists(sourcedirectory))             {                 if (Directory.Exists(destinationdirectory))                 {                     //Directory.Delete(destinationdirectory);                     Directory.Move(destinationdirectory, backupdirectory + DateTime.Now.ToString("_MMMdd_yyyy_HHmmss"));                     Directory.Move(sourcedirectory, destinationdirectory);                 }                 else                 {                     Directory.Move(sourcedirectory, destinationdirectory);                 }             }          }         catch (Exception ex)         {             Console.WriteLine(ex.Message);         }         Console.ReadLine();     } } 
like image 196
Mark Hall Avatar answered Sep 28 '22 17:09

Mark Hall