Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.Move doesn't work (file already exist)

Tags:

I've got main folder:

c:\test 

And there I have 2 folders: Movies and Photos.

Photos has three folders with files with the same structure: People, Animals and Buildings. I'm trying this code:

Directory.Move(@"c:\test\Movies", @"c:\test\Test"); 

I get exception:

File already exists 
like image 222
user278618 Avatar asked Mar 31 '10 12:03

user278618


People also ask

Will file move overwrite?

File. Move() doesn't support overwriting of an existing file.

Can not create a file when that file already exists?

This is because you're trying to fake the existence of the Backup folder (so it must not exist already), but you also need its parents to exist. Windows will hence create a hard link shortcut Backup on the C:\ drive.

How do I move a file to a different location?

Right-click the file or folder you want, and from the menu that displays click Move or Copy. The Move or Copy window opens. Scroll down if necessary to find the destination folder you want.


2 Answers

This method will move content of a folder recursively and overwrite existing files.
You should add some exception handling.
Edit:
This method is implemented with a while loop and a stack instead of recursion.

public static void MoveDirectory(string source, string target) {     var stack = new Stack<Folders>();     stack.Push(new Folders(source, target));      while (stack.Count > 0)     {         var folders = stack.Pop();         Directory.CreateDirectory(folders.Target);         foreach (var file in Directory.GetFiles(folders.Source, "*.*"))         {              string targetFile = Path.Combine(folders.Target, Path.GetFileName(file));              if (File.Exists(targetFile)) File.Delete(targetFile);              File.Move(file, targetFile);         }          foreach (var folder in Directory.GetDirectories(folders.Source))         {             stack.Push(new Folders(folder, Path.Combine(folders.Target, Path.GetFileName(folder))));         }     }     Directory.Delete(source, true); } public class Folders {     public string Source { get; private set; }     public string Target { get; private set; }      public Folders(string source, string target)     {         Source = source;         Target = target;     } } 

Update:
This is a simpler version with the use of Directory.EnumerateFiles recursively instead of using a stack.
This will only work with .net 4 or later, to us it with an earlier version of .net change Directory.EnumerateFiles to Directory.GetFiles.

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); } 
like image 136
Jens Granlund Avatar answered Sep 28 '22 20:09

Jens Granlund


The destination directory should not already exist - the Directory.Move method creates the destination directory for you.

like image 33
Fiona - myaccessible.website Avatar answered Sep 28 '22 19:09

Fiona - myaccessible.website