Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no Directory.Copy in C# [closed]

Tags:

c#

.net

directory

I am making a program in C# with a lot of IO operations. Some of that operations are copying directories.

I was really stunned when I figured out that you don't have something like Directory.copy(SourceDir, DestinationDir) in C#.

I googled a little bit around and at msdn they give a code sample to copy directories. (http://msdn.microsoft.com/en-us/library/bb762914.aspx). But when you search a little bit further, there is a Directory.Copy method in the Microsoft.VisualBasic.FileIO namespace. (http://msdn.microsoft.com/en-us/library/ms127957.aspx)

I could refere to this namespace and use this method, but there must be a reason why Microsoft does not support this in C# and why they aren't mentioning it on msdn.

Hope somebody can tell me the reason.

I can write extensionmethod to solve this problem & I can solve it pretty easy, but my question is Why? Why is there no such method in C#, I just want to know :-)

like image 231
Bram Van Strydonck Avatar asked Feb 26 '13 15:02

Bram Van Strydonck


People also ask

How do I copy a directory?

Alternatively, right-click the folder, select Show more options and then Copy. In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents.

Does cp create directory if not exists?

If you specify the -L option, docker cp follows any symbolic link in the SRC_PATH . docker cp does not create parent directories for DEST_PATH if they do not exist. Error condition: the destination directory must exist.


1 Answers

Because it's easy enough to do a foreach on a DirectoryInfo.GetFiles(), while also giving you an opportunity to filter the list of files being copied, or do some other operation besides copying.

If it really bothers you, write an extension method for the DirectoryInfo class, or a FileInfo[] collection.

like image 150
Robert Harvey Avatar answered Oct 18 '22 12:10

Robert Harvey