Skip to main content

Delete Folder and Its Content using C#


In this tutorial, i will going to teach you how to create a folder and its content using C#. I always got an error in creating this program prompted as "Directory is not empty" and it cannot be deleted. So, I came up this program and studied again the possible code to fix this.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and name your project as Delete Folder.

2. Next, add only one Button named Button1 and labeled it as "Delete Folder". Add also TextBox named TextBox1 for our inputting the path of the folder that you want to delete. You must design your interface like this:

design

3. To achieve that, first of all we need to include the following statement in the program code:

using System.IO - This namespace has to precede the whole program code as it is higher in hierarchy for DirectoryInfo. In Fact, this is the concept of object oriented programming where DirectoryInfo is part of the namespace System.IO.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. using System.IO;

4. Now, to avoid an error saying Directory is not empty when we delete a folder, we will create a method named DeleteDirectory with string target_dir as String as parameter.

  1. public static void DeleteDirectory(string target)
  2. {
  3. string[] files = Directory.GetFiles(target);
  4. string[] dir = Directory.GetDirectories(target);
  5.  
  6. foreach (string file in files)
  7. {
  8. File.SetAttributes(file, FileAttributes.Normal);
  9. File.Delete(file);
  10. }
  11.  
  12. foreach (string dir in dir)
  13. {
  14. DeleteDirectory(dir);
  15. }
  16.  
  17. Directory.Delete(target, true);
  18. }

The syntax Directory.Delete(target, true); will delete all the contents of the folder directory. If false, then the content will not be deleted.

5. Now to delete the folder, we will now code for Button1. We will just use the method above.

  1. private void Button1_Click(object sender, EventArgs e)
  2. {
  3. DirectoryInfo di = new DirectoryInfo(TextBox1.Text);
  4. if (di.Exists)
  5. {
  6.  
  7. DeleteDirectory(TextBox1.Text);
  8. MessageBox.Show("The directory was deleted successfully");
  9. TextBox1.Clear();
  10. }
  11. else
  12. {
  13. MessageBox.Show(" Path is not found!", "Error" );
  14. TextBox1.Clear();
  15. }
  16. }

We have created an instance of DirectoryInfo that holds the path inputted in textbox1. If the directory is existed, then we have used the method DeleteDirectory and its textbox1 as the parameter. Otherwise, it will prompt the user that the path was not found.

Press F5 to run the program.

Output:

output

Full source code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. using System.IO;
  10.  
  11. namespace Delete_Folder
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. public static void DeleteDirectory(string target)
  20. {
  21. string[] files = Directory.GetFiles(target);
  22. string[] dir = Directory.GetDirectories(target);
  23.  
  24. foreach (string file in files)
  25. {
  26. File.SetAttributes(file, FileAttributes.Normal);
  27. File.Delete(file);
  28. }
  29.  
  30. foreach (string dir in dir)
  31. {
  32. DeleteDirectory(dir);
  33. }
  34.  
  35. Directory.Delete(target, true);
  36. }
  37.  
  38. private void Button1_Click(object sender, EventArgs e)
  39. {
  40. DirectoryInfo di = new DirectoryInfo(TextBox1.Text);
  41. if (di.Exists)
  42. {
  43.  
  44. DeleteDirectory(TextBox1.Text);
  45. MessageBox.Show("The directory was deleted successfully");
  46. TextBox1.Clear();
  47. }
  48. else
  49. {
  50. MessageBox.Show(" Path is not found!", "Error" );
  51. TextBox1.Clear();
  52. }
  53. }
  54. }
  55. }

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.