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

后端 未结 7 945
误落风尘
误落风尘 2020-12-06 10:19

Why is it that File.Move(sourceFileName, destFileName) works fine when the source file and destination files are in different partitions, but Directory.Mo

相关标签:
7条回答
  • 2020-12-06 10:36

    Based on the posts "Copy a directory to a different drive" and "Non-recursive way to get all 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);
        }
    
    0 讨论(0)
  • 2020-12-06 10:41

    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.

    0 讨论(0)
  • 2020-12-06 10:44

    I had same problem in VB.NET and instead of "Directory.Move" I used MoveFolder with "FileSystemObject". You can preserve creation dates with this method.

    Scripting.FileSystemObject oFSO = new Scripting.FileSystemObject();
    oFSO.MoveFolder(sourceDirName, destDirName)
    
    0 讨论(0)
  • 2020-12-06 10:52

    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);
    
    0 讨论(0)
  • 2020-12-06 10:58

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

    Code:

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

    This works on different volume seamlessly/ per my experience.

    0 讨论(0)
  • 2020-12-06 10:59

    You can also p/invoke SHFileOperation which is the same function Windows Explorer uses to move directories around. It will either perform a true move or recursive-copy-then-delete, as appropriate.

    It can also show the same progress UI as explorer, just by setting a flag.

    0 讨论(0)
提交回复
热议问题