delete a directory in csharp

To delete a directory in C#, you can use the Directory.Delete() method. Here's the code:

main.cs
using System.IO;

// ...

string directoryPath = "path/to/directory";
if (Directory.Exists(directoryPath))
{
    Directory.Delete(directoryPath);
}
148 chars
10 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.cs
Directory.Delete(directoryPath, true);
39 chars
2 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