Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Move will not work across volumes" - Why? And how to overcome?

Tags:

c#

file

directory

Why is it that File.Move(sourceFileName, destFileName) works fine when the source file and destination files are in different partitions, but Directory.Move(sourceDirName, destDirName) don't? It throws

System.IO.IOException: "Source and destination path must have identical roots. Move will not work across volumes."

I even tried to create a DirectoryInfo instance and use the MoveTo(destDirName) method but without success.

Am I missing something? Do I really have to implement a "move" functionality myself? (the directory I want to move is very large btw).

like image 837
first timer Avatar asked May 04 '15 03:05

first timer


2 Answers

You should Use Copy Function followed by a remove. As Move only works in the same drive. Directory.Move has a condition that states that :

IO Exception will be thrown if an attempt was made to move a directory to a different volume.

like image 93
Ehsan Avatar answered Nov 02 '22 15:11

Ehsan


Another option is, to add a reference to the Microsoft.VisualBasic namespace and use the MoveDirectory method, which can move across volumes.

Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(sourceDirName, destDirName);
like image 36
NePh Avatar answered Nov 02 '22 13:11

NePh