Udara S.S Liyanage
Udara S.S Liyanage

Reputation: 6453

Why .net does not have method to copy an entire directory

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.

Upvotes: 4

Views: 240

Answers (2)

Ergwun
Ergwun

Reputation: 12978

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).

Upvotes: 3

phoog
phoog

Reputation: 43046

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.

Upvotes: 4

Related Questions