Extension methods: Copy all files in a directory (recursive)
Extension methods are just so much fun. Somebody over at Channel 9 asked about a solution to copy all files from one directory to another. I first provided a solution that did it in C# 2.0. But why not create one that is nicer, done in C# 3.0 and uses extension methods? And that’s what I got:
{
// Copies all files from one directory to another.
public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive)
{
if (source == null)
throw
if (destDirectory == null)
throw
// Compile the target.
DirectoryInfo target =
// If the source doesn’t exist, we have to throw an exception.
if (!source.Exists)
throw
// If the target doesn’t exist, we create it.
if (!target.Exists)
target.Create();
// Get all files and copy them over.
foreach (FileInfo file in source.GetFiles())
{
file.CopyTo(Path.Combine(target.FullName, file.Name), true);
}
// Return if no recursive call is required.
if (!recursive)
return;
// Do the same for all sub directories.
foreach (DirectoryInfo directory in source.GetDirectories())
{
CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive);
}
}
}
The usage is the following:
source.CopyTo(“C:\\users\\chris\\desktop_backup”, true);
Extension methods: I love you! ![]()






Published on Sep 8th, 2007 —
Tags:
[...] Extension methods: Copy all files in a directory (recursive) [...]
Pingback by Why I changed my mind about Extension Methods - James Newton-King — February 4, 2008 @ 8:37 am
Hi,
Was any testing done to find out just how big the directories can be (# of files) or just how many levels they can be nested before the recursion craps out due to stack full?
Comment by sherifffruitfly — March 5, 2008 @ 6:56 am
I haven’t tested that. But you could do it…
Keep in mind that the recursion is only limited to the maximum of nested directories… It’s not depending on how many files are found in the directories.
Comment by Christian Liensberger — March 5, 2008 @ 12:12 pm
“Keep in mind that the recursion is only limited to the maximum of nested directories… It’s not depending on how many files are found in the directories.”
Understood, but the memory allocated to hold all of those indiviual files isn’t released until the recursion is popped out of, right? That is, each source.getfiles() collection hangs around iteration after iteration until the *entire* recursion is done., no?
Comment by sherifffruitfly — March 5, 2008 @ 11:13 pm
One way to alleviate my worry would be to set source = null after the copyto loop finishes.
Assuming it’s worth worrying about at all.
Comment by sherifffruitfly — March 5, 2008 @ 11:15 pm
Attention: this is C# and not C++. We have a Garbage Collector running here and all objects (except structs) are allocated on the Heap and not on the Stack!
The FileInfos that are returned in the foreach loop have only a local scope and the Garbage Collector knows that. That means that after the scope all the FileInfo objects returned by GetFiles() have already been destroyed by the GC.
Also, GetFiles doesn’t hold the FileInfo objects internally. It just returns them.
Setting source to null is unneccessary. The GC doesn’t need that because all local pointers to objects are cleared anyway after exiting the method.
When you work with C# you should keep in mind that the GC creates a reachable tree to understand what variables are still reachable from your code. All others are automatically destroyed by the GC. That’s the benefit of adding a garbage collector to the game: the memory management get’s automated and you don’t need to worry about it anymore. The only thing where you should keep attention is when you use static variables. These are never deleted (until you terminate the application) because you could reach them from everywhere in your application.
Comment by Christian Liensberger — March 8, 2008 @ 10:23 am