Delete a folder even if it is with files

Asked

Viewed 2,330 times

1

What code should I use to delete a folder where it contains files within it?

This code I used only erases an empty folder.


void ApagarPasta(string nameOf, bool subPastas){
     Directory.Delete(nameOf, subPastas); // Deleta a pasta e subpastas vazias apenas.
}
  • 1

    subfolders is true? It needs to be to delete recursively.

  • Didn’t understand, the answer solved your problem? If "solved", it seems that you had no problem.

1 answer

2


The second parameter of the method Delete indicates whether sub-folders/files should also be removed.

public static void Delete(
    string path,
    bool recursive
)

path Type: System.String
The name of the directory to be removed.

recursive Type: System.Boolean
true to remove directories, subdirectories, and path files; otherwise, false.

Example:

System.IO.Directory.Delete("caminho da pasta", true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.