Here's an extension method based on the original OPs code, which I think is just fine and a bit more readable than other options.
I agree that it would be nice to have one method in the structure for deleting the contents of a directory without deleting the directory, but, in my opinion, this is the next best thing.
using System; using System.IO; namespace YourNamespace { public static class DirectoryInfoExtensions { public static void EmptyDirectory(this DirectoryInfo di) { if (di.Exists) { foreach (var file in di.GetFiles()) { file.Delete(); } foreach (var directory in di.GetDirectories()) { directory.Delete(true); } } } } }
Steve lautenschlager
source share