• We just launched and are currently in beta. Join us as we build and grow the community.

Delete Folder and Its Content using C#

Hatsune

Battle Tactician
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
159
Likes
72
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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:

deldesign.png


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. 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. foreach

    (

    string

    file in

    files)
  6. {
  7. File.

    SetAttributes

    (

    file, FileAttributes.

    Normal

    )

    ;
  8. File.

    Delete

    (

    file)

    ;
  9. }

  10. foreach

    (

    string

    dir in

    dir)
  11. {
  12. DeleteDirectory(

    dir)

    ;
  13. }

  14. Directory.

    Delete

    (

    target, true

    )

    ;
  15. }

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. DeleteDirectory(

    TextBox1.

    Text

    )

    ;
  7. MessageBox.

    Show

    (

    "The directory was deleted successfully"

    )

    ;
  8. TextBox1.

    Clear

    (

    )

    ;
  9. }
  10. else
  11. {
  12. MessageBox.

    Show

    (

    " Path is not found!"

    , "Error"

    )

    ;
  13. TextBox1.

    Clear

    (

    )

    ;
  14. }
  15. }

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:
delout.png


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. using

    System.IO

    ;

  9. namespace

    Delete_Folder
  10. {
  11. public

    partial

    class

    Form1 :

    Form
  12. {
  13. public

    Form1(

    )
  14. {
  15. InitializeComponent(

    )

    ;
  16. }
  17. public

    static

    void

    DeleteDirectory(

    string

    target)
  18. {
  19. string

    [

    ]

    files =

    Directory.

    GetFiles

    (

    target)

    ;
  20. string

    [

    ]

    dir =

    Directory.

    GetDirectories

    (

    target)

    ;

  21. foreach

    (

    string

    file in

    files)
  22. {
  23. File.

    SetAttributes

    (

    file, FileAttributes.

    Normal

    )

    ;
  24. File.

    Delete

    (

    file)

    ;
  25. }

  26. foreach

    (

    string

    dir in

    dir)
  27. {
  28. DeleteDirectory(

    dir)

    ;
  29. }

  30. Directory.

    Delete

    (

    target, true

    )

    ;
  31. }

  32. private

    void

    Button1_Click(

    object

    sender, EventArgs e)
  33. {
  34. DirectoryInfo di =

    new

    DirectoryInfo(

    TextBox1.

    Text

    )

    ;
  35. if

    (

    di.

    Exists

    )
  36. {

  37. DeleteDirectory(

    TextBox1.

    Text

    )

    ;
  38. MessageBox.

    Show

    (

    "The directory was deleted successfully"

    )

    ;
  39. TextBox1.

    Clear

    (

    )

    ;
  40. }
  41. else
  42. {
  43. MessageBox.

    Show

    (

    " Path is not found!"

    , "Error"

    )

    ;
  44. TextBox1.

    Clear

    (

    )

    ;
  45. }
  46. }
  47. }
  48. }

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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,441

337,449

Top