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

后端 未结 6 563
逝去的感伤
逝去的感伤 2020-11-30 06:42

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 s

相关标签:
6条回答
  • 2020-11-30 07:08

    You can use move method directly.

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

    The folder will be deleted and copied it into Test Folder.

    0 讨论(0)
  • 2020-11-30 07:12
    ProcessStartInfo p = new ProcessStartInfo("cmd", "/c move \"c:\\test\\Movies\" \"c:\\test\Test\\"");
    p.WindowStyle = ProcessWindowStyle.Hidden; //hide mode
    Process.Start(p);
    
    0 讨论(0)
  • 2020-11-30 07:24

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

    0 讨论(0)
  • 2020-11-30 07:24

    Is it safe for you to delete the destination folder before copying new contents to it?

        Directory.Delete(@"c:\test\test");
        Directory.Move(@"c:\test\movies",@"c:\test\test");
    
    0 讨论(0)
  • 2020-11-30 07:27

    The most common 2 reasons why Directory.Move could fail are, if:

    • It's a different volume (you need to Copy/Delete)
    • It already exists (doesn't support overwrite by default)

    Here is my simple solution for the second problem (overwrite):

    public bool MoveDirectory(string sourceDirName, string destDirName, bool overwrite)
    {
        if (overwrite && Directory.Exists(destDirName))
        {
            var needRestore = false;
            var tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            try
            {
                Directory.Move(destDirName, tmpDir);
                needRestore = true; // only if fails
                Directory.Move(sourceDirName, destDirName);
                return true;
            }
            catch (Exception)
            {
                if (needRestore)
                {
                    Directory.Move(tmpDir, destDirName);
                }
            }
            finally
            {
                Directory.Delete(tmpDir, true);
            }
        }
        else
        {
            Directory.Move(sourceDirName, destDirName); // Can throw an Exception
            return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-30 07:30

    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);
    }
    
    0 讨论(0)
提交回复
热议问题