views:

454

answers:

2

Hi,

Apparently I can't move files on different volumes using Directory.Move.

I have read that I have to copy each file individually to the destination, then delete the source directory.

Do I have any other option?

+3  A: 

Regardless of whether or not Directory.Move (or any other function) performed the move between volumes, it would essentially be doing a copy and delete anyway underneath. So if you want a speed increase, that's not going to happen. I think the best solution would be to write your own reusable move function, which would get the volume label (C:,D:) from the to and from paths, and then either perform a move, or copy+delete when necessary.

Kibbee
+2  A: 

To my knowledge there is no other way however deleting a directory has a catch: Read Only Files might cause a UnauthorizedAccessException greeve when deleting a directory and all of its contents

 public void removeReadOnlyDeep(string directory)
    {
        string[] files = Directory.GetFiles(directory);
        foreach (string file in files)
        {
            FileAttributes attributes = File.GetAttributes(file);
            if ((attributes & FileAttributes.ReadOnly) != 0)
            {
                File.SetAttributes(file, ~FileAttributes.ReadOnly);
            }
        }
        string[] dirs = Directory.GetDirectories(directory);
        foreach (string dir in dirs)
        {
            removeReadOnlyDeep(dir);
        }
    }

this recurses a directory and unsets all the read only flags. Call before Directory.Delete :)

Martijn Laarman