2

In .net Directory class have a method to Move, Delete directories. However it does does not have a method to Copy an directory and its content. We have to copy each file and subdirectory using a loop as this How to: Copy Directories

I want to know what is the rational/ reason behing NOT having a Copy method.

Udara S.S Liyanage
  • 6,189
  • 9
  • 33
  • 34
  • http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c-sharp – Habib Nov 29 '12 at 05:04

2 Answers2

4

This is because Windows API MoveFile and related functions can also be used to move directories, but CopyFile and related functions cannot be used with directories.

More fundamentally, this is because moving a file or directory is just renaming it; it doesn't actually require physically moving the file's (or files') data on the disk. When you rename a directory, the files that it contains automatically pick up the new path "by reference", as it were. It's not necessary to operate on each file's entry.

phoog
  • 42,068
  • 6
  • 79
  • 117
3

It's not that .NET does not have a method to copy an entire directory (since there exists one in VB.NET), it's that C# does not have a method to copy an entire directory.

Given that this feature is supported in other .NET languages, it seems unlikely that there are technical or philosophical reasons for its absence in C#, and we can fall back on the null hypothesis that "by default features don't exist" (as oft explained by Eric Lippert).

Ergwun
  • 12,579
  • 7
  • 56
  • 83
  • 1
    But any method that exists in VB.NET exists in C#, as long as you reference the correct assembly. The real question is that *the Directory class* does not have a method to copy an entire directory, and that's the question the OP asked. – phoog Nov 29 '12 at 05:59
  • 2
    @phoog - Sure, you can use that VB.NET method in C#. Your answer about the functions available in the Windows API is interesting, and can certainly contribute to the cost of implementing this feature, but it's still only one part of the reason for the feature not being available, and I was just trying to point the OP towards the bigger picture. – Ergwun Nov 29 '12 at 07:30
  • Of course -- and pointing out the VB.NET method is a helpful addition to the discussion, as is the idea that "by default features don't exist". It might be worthwhile to add that VB comes from a philosophy in which the language allows the programmer to focus more on higher-level concerns; this explains the motivation to provide the function in the VB library (and indeed, the `My` namespace itself). – phoog Nov 29 '12 at 18:09