Yesterday, I saw a person posted a question on forums “How to delete all the files and folders inside a folder” with C#. I thought I should blog for it. So I made a sample project to demonstrate the same. I made a extension method which will delete all the files and folders inside a particular folder. Before going further you should read this:
What are Extension Methods?
Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. Extension Methods help blend the flexibility of "duck typing" support popular within dynamic languages today with the performance and compile-time validation of strongly-typed languages.
Extension Methods enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework that is being introduced with .NET as part of the "Orcas" release.
Now come to the code. The class which contains the extension method:
#region Using Directives
public static class FileExtensions
public static void DeleteAllFoldersAndFiles(this DirectoryInfo directoryInfo)
foreach (FileInfo fileInfo in directoryInfo.GetFiles()) fileInfo.Delete();
foreach (DirectoryInfo childDirectories in directoryInfo.GetDirectories()) childDirectories.Delete(true);
Above in the first line of DeleteAllFoldersAndFiles method I am deleting all the files and in the second line I am recursively deleting all the folders with all the files in it.
So how will we use it?
Here is the code which shows its implementation:
#region Using Directives
public class Example
public static void Main()
const string dummyPathToDelete = @"C:\DummyFolder";
var directory = new DirectoryInfo(dummyPathToDelete);
Above I have used the hardcoded path dummyPathToDelete in my example, you can use your own logic to decide the path. Then I gave the reference of the class where my extension method resides and that’s it I am ready to use DeletAllFoldersAndFiles method with this code;
Do let me know your feedback, comments.
Here a quick hitter for you - this little piece of code allows you to validate a credit card (in C#) before submitting for processing. This should work with Visa, Masterc...
You may come across an instance in your C# and ASP.NET programming where you need to download an external webpage and parse the meta tags... specifically, the "Title...
C# RSS FEED GENERATOR - This is a snippet of code that you can simply paste into your own code to generate a C# RSS
If you want to remove invalid chars "* . " / \ [ ] : ; | = ," from file name below is the code which can help you Sample code: using System; using System.I...
In this blog I will show you how to swap two integer variables. I will be using Interlocked.Exchange to do the same: using System; using System.Threading; namespace Sampl...
In this blog I will show how to replace single backslash with double backslash in a string with REGEX. Let's say we have a string "This is \ test \ string" ...
What kind of email newsletter would you prefer to receive from CodeAsp.Net?