Using system. IO; Public static void deletedir (string aimpath) { Try { // Check whether the target directory ends with a directory delimiter. If not, add it. If (aimpath [aimpath. Length-1]! = Path. directoryseparatorchar) Aimpath + = path. directoryseparatorchar; // Obtain the file list of the source Directory, which is an array containing the file and directory path // Use the following method if you direct to the file under the delete target file without including the Directory // String [] filelist = directory. getfiles (aimpath ); String [] filelist = directory. getfilesystementries (aimpath ); // Traverse all files and directories Foreach (string file in filelist) { // Process the file as a directory first. If this directory exists, recursively delete the files under this directory. If (directory. exists (File )) { Deletedir (aimpath + path. getfilename (File )); } // Otherwise, delete the file directly. Else { File. Delete (aimpath + path. getfilename (File )); } } // Delete a folder System. Io. Directory. Delete (aimpath, true ); } Catch {
} } |
Example:
Protected void btndelete_click (Object sender, imageclickeventargs E)
{
VaR ROW = This. ultrawebgrid1.displaylayout. activerow;
String brname = (string) Row. cells. fromkey ("name"). value;
String filename = server. mappath ("../");
String pathname = filename + @ "backupandrestore/" + brname;
File. Delete (pathname );
Response. Write ("<SCRIPT type = 'text/JavaScript '> window. Location = 'frmdbbackup. aspx'; </SCRIPT> ");
}