System.IO.Directory.Copy()?
Answer me this, .NET gurus: <primalscream>Why</primal_scream> does the System.IO.Directory class not include a .Copy() method. I can Move(), CreateDirectory(), and even Delete(), but I can't copy a directory and all of its contents recursively? And don't even try to tell me that Directory.Copy() doesn't exist because the .NET team figured that even wannabe programmers like me can create our own .copy() as easily as we can type D-i-r-e-c-t-o-r-y-.-C-o-p-y-(-).
My attempts to workaround this limitation went like this:
- In Visual Studio .NET, I typed System.IO.Directory and press the F1 key. Result: learned a lot about the Directory class that didn't help me accomplish the task before me.
- Searched for Index for keyword “directory.copy“ in Visual Studio .NET. Result: failure.
- Searched VS.NET help for “Directory.Copy“. Result: failure.
- Created my own dir.Copy() ... Result: short attention span...disbelief...decided to consult my friend Google.
- Searched Google for “Directory.Copy() +.NET“. Result: multiple successful hits. Copied sample code snippets* from Web to my project and was successfully copying directories recursively several minutes later.
Takeaways
- Directory.Copy() should exist, and I am by no means the only .NET developer in the world who thinks this[1].
- “Directory.Copy” and any other similarly inexplicable class exclusions should in future be added to the MSDN Combined Collection's index, as should a topic that explains why it is not available in the .NET Framework.
[1]https://www.codeproject.com/csharp/copydirectoriesrecursive.asp
https://www.aspemporium.com/aspEmporium/cshrp/howtos/howto.asp?hid=20
And I don't know what this says exactly, but my issue is an international one [https://dobon.net/vb/dotnet/file/copyfolder.html]:
????????Directory.Move??????????????????"Directory.Copy"???????????????.NET Framework????????????????File.Copy????????????????????????
???????????????????(CopyDirectory????)??????????????????????????????
Aha! This just in [from Kit George]: The .NET team is thinking about adding a Copy method to the System.IO.Directory class for Whidbey. https://weblogs.asp.net/bclteam/archive/2004/01/19/60377.aspx
Comments
Anonymous
February 19, 2004
Grrr. .Text displays extended characters in the editor but not on the blog display page. Note to self: must note as SuggestedFeature in .Text wiki.Anonymous
February 19, 2004
I was gonna say... I saw a blog post a couple of weeks ago about this from someone on the theam and they gave the same answer. It does make sense, but certainly if you can ship something as complex as a DataGrid you can copy a directory. :)Anonymous
February 19, 2004
Could it have something to do with what Junfeng Zhang recently posted about (http://blogs.msdn.com/junfeng/archive/2004/02/20/76467.aspx)?Anonymous
February 22, 2004
+1Anonymous
February 22, 2004
I had pretty much the same reaction when lookin for Directory.Rename() or File.Rename()Anonymous
March 10, 2004
My good friend Justin sends me another C# snippet to copy dirs recursively. Thanks Justin. Your copy()'s more elegant than my copy().;-)
using System;
using System.IO;
namespace Utility.IO
{
public class Filesystem
{
// Copy directory structure recursively
public static void copyDirectory(string src, string dst)
{
String[] files;
if(dst[dst.Length-1]!=Path.DirectorySeparatorChar)
dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(dst)) Directory.CreateDirectory(dst);
files=Directory.GetFilesystemEntries(src);
foreach(string element in files)
{
// Sub directories
if(Directory.Exists(element))
copyDirectory(element,dst+Path.GetFileName(element));
// files in directory
else
File.Copy(element,dst+Path.GetFileName(element),true);
}
}
}
}Anonymous
March 31, 2004
Would the CopyFile API call do this more efficiently than the above code?
http://www.webtropy.com/articles/art9-1.asp?f=CopyFileAnonymous
May 11, 2004
You have to add Reference from COM Object:
"Microsoft Scripting Runtime"
And then just use the following code:
Private Sub CopyDir()
Dim strSourcePath, strDestinationPath As String
Dim mDirectory As New Scripting.FileSystemObject
'parameter "True" means overwrite if exsts
mDirectory.CopyFolder(strSourcePath, strDestinationPath, True)
End SubAnonymous
June 02, 2004
Of course, everybody used the Microsoft Scripting Runtime in VB6 to copy directories.
but my first law : No COM in .NET !
disallows me to use any COM in .NET :) .
So i will wait til Microsoft will include
System.IO.Directory.Copy!Anonymous
June 24, 2004
What's especially irritating is that the MS documentation explicitly states that the Directory class does provide a copy capability! From http://tinyurl.com/2r3ad:
"Use the Directory class for typical operations such as copying, moving, renaming, creating, and deleting directories"Anonymous
June 27, 2004
The comment has been removedAnonymous
July 13, 2004
I was looking for the same thing. Thank you for the code.Anonymous
May 27, 2005
' copy the entire contents of the C:DOCS directory into the
' C:BACKUPDOCS directory, overriding existing files.
'
' This code assumes that you added a reference to the
' Microsoft Scripting Runtime type library
that is in the COM Project Reference
Dim fso As New Scripting.FileSystemObject
fso.CopyFolder "c:docs", "c:backupdocs", TrueAnonymous
December 29, 2005
Here's a slightly cleaner version of the code:
public static void copyDirectory(DirectoryInfo srcPath, DirectoryInfo destPath, bool recursive)
{
if( !destPath.Exists )
destPath.Create();
// copy files
foreach( FileInfo fi in srcPath.GetFiles() )
{
fi.CopyTo( Path.Combine(destPath.FullName, fi.Name), true );
}
// copy directories
if( recursive )
{
foreach( DirectoryInfo di in srcPath.GetDirectories() )
{
copyDirectory( di, new DirectoryInfo( Path.Combine( destPath.FullName, di.Name ) ), recursive );
}
}
}Anonymous
March 23, 2006
Just wanted to say thanks; your efforts have saved me time! Nice work...Anonymous
May 25, 2006
This seem to do what you want
http://msdn2.microsoft.com/en-us/library/xz2d9afk.aspxAnonymous
July 16, 2006
it works! thanksAnonymous
August 26, 2006
Thanks for the great tips about <a href="http://home-owner.kogaryu.com/black-jack-online-play.html"">http://home-owner.kogaryu.com/black-jack-online-play.html" title="black jack online play">black jack online play</a> and [URL=http://home-owner.kogaryu.com/black-jack-online-play.html]black jack online play[/URL]Anonymous
August 27, 2006
Thanks for the great tips about <a href="http://approvalcredit.bravehost.com/holiday-ski.html"">http://approvalcredit.bravehost.com/holiday-ski.html" title="holiday ski">holiday ski</a> and [URL=http://approvalcredit.bravehost.com/holiday-ski.html]holiday ski[/URL]Anonymous
August 27, 2006
Very informative post about <a href="http://debtin.50megs.com/cheap-loan-personal.html"">http://debtin.50megs.com/cheap-loan-personal.html" title="cheap loan personal">cheap loan personal</a> and [URL=http://debtin.50megs.com/cheap-loan-personal.html]cheap loan personal[/URL]Anonymous
August 27, 2006
Thanks for the great tips about <a href="http://approvalcredit.bravehost.com/gambling-game.html"">http://approvalcredit.bravehost.com/gambling-game.html" title="gambling game">gambling game</a> and [URL=http://approvalcredit.bravehost.com/gambling-game.html]gambling game[/URL]Anonymous
August 27, 2006
Thank you for this great post about <a href="http://eteamz.active.com/personalloan/files/adverse-credit-remortgage.html"">http://eteamz.active.com/personalloan/files/adverse-credit-remortgage.html" title="adverse credit remortgage">adverse credit remortgage</a> and [URL=http://eteamz.active.com/personalloan/files/adverse-credit-remortgage.html]adverse credit remortgage[/URL]Anonymous
September 01, 2006
Hi baby!Anonymous
September 04, 2006
Thank you for this great post about <a href="http://eteamz.active.com/debtsolution/files/ecommerce-host.html"">http://eteamz.active.com/debtsolution/files/ecommerce-host.html" title="ecommerce host">ecommerce host</a> and [URL=http://eteamz.active.com/debtsolution/files/ecommerce-host.html]ecommerce host[/URL]Anonymous
September 05, 2006
The comment has been removedAnonymous
September 05, 2006
Thank you so much for this great post about <a href="http://getfreecredit.bravehost.com/debt-management-counseling.html"">http://getfreecredit.bravehost.com/debt-management-counseling.html" title="debt management counseling">debt management counseling</a> and [URL=http://getfreecredit.bravehost.com/debt-management-counseling.html]debt management counseling[/URL]Anonymous
September 05, 2006
Thank you so much for this great post about <a href="http://eteamz.active.com/debtsolution/files/domain-register.html"">http://eteamz.active.com/debtsolution/files/domain-register.html" title="domain register">domain register</a> and [URL=http://eteamz.active.com/debtsolution/files/domain-register.html]domain register[/URL]Anonymous
September 16, 2006
[URL=http://http://replicarolexwatch.ir.pl]replica-rolex-watch[/URL]<a href="http://http://replicarolexwatch.ir.pl">replica rolex watch</a>Anonymous
September 21, 2006
The comment has been removedAnonymous
March 28, 2007
The comment has been removedAnonymous
June 09, 2009
PingBack from http://insomniacuresite.info/story.php?id=1937Anonymous
June 12, 2009
PingBack from http://toenailfungusite.info/story.php?id=6971Anonymous
June 19, 2009
PingBack from http://edebtsettlementprogram.info/story.php?id=22629Anonymous
June 19, 2009
PingBack from http://debtsolutionsnow.info/story.php?id=2782