Delete all contents in a folder in Asp.net C #

Source: Internet
Author: User

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> ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.