We know in c #, if a folder contains content, we can directly use Directory. Delete (folder); but it cannot be deleted. How can we Delete it? The following small method can help you.
View plaincopy to clipboardprint?
Public void DeleteFolder (string deleteDirectory)
{
If (Directory. Exists (deleteDirectory ))
{
Foreach (string deleteFile in Directory. GetFileSystemEntries (deleteDirectory ))
{
If (File. Exists (deleteFile ))
File. Delete (deleteFile );
Else
DeleteFolder (deleteFile );
}
Directory. Delete (deleteDirectory );
}
}
Recursively delete the files in the folder and then delete the empty folder.
Public void DeleteFolder (string deleteDirectory)
{
If (Directory. Exists (deleteDirectory ))
{
Foreach (string deleteFile in Directory. GetFileSystemEntries (deleteDirectory ))
{
If (File. Exists (deleteFile ))
File. Delete (deleteFile );
Else
DeleteFolder (deleteFile );
}
Directory. Delete (deleteDirectory );
}
}
Recursively delete the files in the folder and then delete the empty folder.
From Poplar