"Moving won't work across volumes" - Why? And how to win?

Why does File.Move(sourceFileName, destFileName) fine when the source file and destination files are in different sections, but Directory.Move(sourceDirName, destDirName) not? He throws

System.IO.IOException: "The source and destination path must have the same roots. Moving will not work across volumes."

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

Am I missing something? Do I really need to implement the move function? (the directory I want to move is very large).

+10
source share
7 answers

You can also p / invoke SHFileOperation , which is the same function that Windows Explorer uses to move directories. It will either perform a true move, or recursively-copy-then-delete, if necessary.

It can also show the same progress user interface as explorer by simply setting a flag.

+2
source

You must use the Copy Function followed by the uninstall. Because Move only works on one drive. Directory.Move has a condition that states that:

An IO exception will be thrown if an attempt was made to move the directory to another volume.

+9
source

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

 Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(sourceDirName, destDirName); 
+4
source

Although this is not a Vb.Net question, but I found that no one mentioned this method, so I think it can help ... Only you need to convert it to C # if necessary.

the code:

 My.Computer.FileSystem.MoveDirectory(SrcDir,DestDir) 

This works on different volumes without interruptions / in my experience.

+2
source

Based on the messages “ Copy a directory to another drive ” and “A non-recursive way to get all the files in a directory and its subdirectories in Java ”, I wrote this non-recursive method and it works fine:

 public static void Move(string source, string target) { if (!Directory.Exists(source)) { throw new System.IO.DirectoryNotFoundException("Source directory couldn't be found."); } if (Directory.Exists(target)) { throw new System.IO.IOException("Target directory already exists."); } DirectoryInfo sourceInfo = Directory.CreateDirectory(source); DirectoryInfo targetInfo = Directory.CreateDirectory(target); if (sourceInfo.FullName == targetInfo.FullName) { throw new System.IO.IOException("Source and target directories are the same."); } Stack<DirectoryInfo> sourceDirectories = new Stack<DirectoryInfo>(); sourceDirectories.Push(sourceInfo); Stack<DirectoryInfo> targetDirectories = new Stack<DirectoryInfo>(); targetDirectories.Push(targetInfo); while (sourceDirectories.Count > 0) { DirectoryInfo sourceDirectory = sourceDirectories.Pop(); DirectoryInfo targetDirectory = targetDirectories.Pop(); foreach (FileInfo file in sourceDirectory.GetFiles()) { file.CopyTo(Path.Combine(targetDirectory.FullName, file.Name), overwrite: true); } foreach(DirectoryInfo subDirectory in sourceDirectory.GetDirectories()) { sourceDirectories.Push(subDirectory); targetDirectories.Push(targetDirectory.CreateSubdirectory(subDirectory.Name)); } } sourceInfo.Delete(true); } 
+1
source

I know this post is a little old ... but there is a way around this! Do not try to move the directory, but archive it and move it as File.Move(src,dest); , and you can extract it, and here you have it!

+1
source

I had the same problem in VB.NET, and instead of "Directory.Move" I used MoveFolder with "FileSystemObject". With this method, you can save the creation dates.

 Scripting.FileSystemObject oFSO = new Scripting.FileSystemObject(); oFSO.MoveFolder(sourceDirName, destDirName) 
0
source

All Articles