5

As the title says, there's a Directory.Move but no Directory.Copy method in Directory class from System.IO. Is there a reason for that?

Update:

To me, the copy and move actions are pretty much identical, the only difference being that the move action does a copy and then deletes the destination. And the error handling is as complex for move as it is for copy. So if one is implemented, why isn't the other?

Update 2:

This is a quote from a comment from mmclean:

Directory.Move however does not move, it renames. Thus the "destination" path is a full path to point to the directory, rather than a location to "move into", and moving to a different drive is impossible.

So I understand that move actually does a rename operation (only changes and entry in the File Allocation Table). But both move and copy commands have the same problem with merging items existing in the destination (overwrite/keep both). So the only added complexity for the copy operation is that it has to physically copy files around. But that still doesn't explain the decision to not implement it. Even more so, when the copy command is implemented in VB.NET and there's a pretty simple implementation for a copy operation on MSDN here.

david.s
  • 11,283
  • 6
  • 50
  • 82
  • This Eric Lippert's answer should answer your question as well: http://stackoverflow.com/a/2806990/1163867 – MarcinJuraszek Aug 30 '13 at 13:01
  • 4
    @MarcinJuraszek - EL is talking about C# features, this question is about the BCL. Two very different things. But a similar logic would be applied. – H H Aug 30 '13 at 13:04
  • in .NET 4.5 there's a revolutionary work around [here](http://msdn.microsoft.com/en-us/library/bb762914.aspx) – Xaqron Aug 30 '13 at 13:15
  • @Xaqron - that's not a workaround but a ready made solution. – H H Aug 30 '13 at 13:18
  • 1
    Move doesn't do a copy and delete. It does a move. Not the same at all. (Unless you're moving across different volumes and then you will get an exception!) Moving a directory only has to update the directory's entries in the File Allocation Table – Matthew Watson Aug 30 '13 at 15:09
  • 1
    Do you want to know why? Or do you want to actually do a copy? – David Heffernan Aug 30 '13 at 15:27
  • @DavidHeffernan I want to know why! I know how to do a copy. – david.s Aug 30 '13 at 15:53

2 Answers2

5

The pragmatic answer is that there is no Windows API call to copy a directory, whereas there is a Windows API call to move (aka rename) a directory (MoveFile()).

Directory.Move() calls MoveFile() as part of its implementation. But there is nothing it can call for copying.

They seem to have kept the interface broadly similar to that provided by the Windows API.

Furthermore, the error handling for a failed directory copy is pretty nasty. Would it have to roll back any changes? How to handle a failed copy is going to be so context-dependent that it's pretty hard to have a general-purpose approach. That's probably why there's no Windows API call to copy a directory too.

Another thing that can cause added complexity is different files being locked during the copy, and files being added and removed from the directory while the copy is taking place.

(Although as noted in another answer here, there's a Visual Basic method to copy a directory, which must address some of these issues.)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • It's more than a few lines if you need to roll back any changes. Think about how you would recover from a failed copy which had partially succeeded (and may have failed because the disk filled up). I suppose you could just make it delete the new directory. – Matthew Watson Aug 30 '13 at 13:16
3

Instead of questioning why, just use the VB.NET's CopyDirectory http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.filesystem.copydirectory.aspx.

AMissico
  • 21,470
  • 7
  • 78
  • 106