To delete a directory in C#, you can use the Directory.Delete()
method. Here's the code:
main.cs148 chars10 lines
This code first checks if the directory exists using Directory.Exists()
method. If it exists, it uses Directory.Delete()
to delete the directory. Note that Directory.Delete()
throws an exception if any file or folder within the directory could not be deleted.
To delete the directory and its contents, including any files or subdirectories, you can use the overload method:
main.cs39 chars2 lines
This code deletes the directory at the path directoryPath
, and sets the second parameter to true
in order to remove any subdirectories or files within that directory.
gistlibby LogSnag