Print? /// <Summary>
/// Use recursive methods to delete folder directories and files
/// </Summary>
/// <Param name = "dir"> path with folder name </param>
Public void DeleteFolder (string dir)
{
If (Directory. Exists (dir) // if this folder Exists, delete it
{
Foreach (string d in Directory. GetFileSystemEntries (dir ))
{
If (File. Exists (d ))
{
FileInfo fi = new FileInfo (d );
If (fi. Attributes. ToString (). IndexOf ("ReadOnly ")! =-1)
Fi. Attributes = FileAttributes. Normal;
File. Delete (d); // Delete the File directly.
}
Else
DeleteFolder (d); // recursively Delete subfolders
}
Directory. Delete (dir, true); // Delete an empty folder
}
}
/// <Summary>
/// Use recursive methods to delete folder directories and files
/// </Summary>
/// <Param name = "dir"> path with folder name </param>
Public void DeleteFolder (string dir)
{
If (Directory. Exists (dir) // if this folder Exists, delete it
{
Foreach (string d in Directory. GetFileSystemEntries (dir ))
{
If (File. Exists (d ))
{
FileInfo fi = new FileInfo (d );
If (fi. Attributes. ToString (). IndexOf ("ReadOnly ")! =-1)
Fi. Attributes = FileAttributes. Normal;
File. Delete (d); // Delete the File directly.
}
Else
DeleteFolder (d); // recursively Delete subfolders
}
Directory. Delete (dir, true); // Delete an empty folder
}
}
Note: The Directory. Delete method can only Delete empty folders. Otherwise, an error is returned. Because of this issue of clearing a folder, it takes a lot of time.