Recursively delete all files in a folder C # code

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO;

Namespace templatefiledelete
{
Public partial class deletefiles: Form
{
Int I = 0;
Public deletefiles ()
{
Initializecomponent ();
}

Private void btopen_click (Object sender, eventargs E)
{
Dealaction (sender, e );
}

Private void btopen2_click (Object sender, eventargs E)
{
Dealaction (sender, e );
}
Public void dealaction (Object sender, eventargs E)
{
Fbdshow. showdialog ();
Button bt = (button) (sender );
If (Bt. Name = "btopen ")
{
This. tbpath1.text = fbdshow. selectedpath;
}
Else if (Bt. Name = "btopen2 ")
{
This. tbpath2.text = fbdshow. selectedpath;
}

}
Public void dirs (string path)
{
Directoryinfo Dis = new directoryinfo (PATH );
Fileinfo [] files = dis. getfiles ();
Foreach (fileinfo fi in files)
{
I ++;
Application. doevents ();
This. label1.text = I. tostring ();
Try
{
File. Delete (Fi. fullname );
}
Catch (exception ex)
{
This. richtextbox1.text + = ex. message;

}

// Getsqlfile (Fi. fullname, this. textbox1.text + fi. directoryname. substring (2), Fi. fullname. substring (Fi. fullname. lastindexof ("\") + 1, 1024,768 );
}
If (DIS. getdirectories (). length> 0)
{
For (INT I = 0; I <dis. getdirectories (). length; I ++)
{
Dirs (DIS. getdirectories () [I]. fullname );
}
}

}

Private void btdelete_click (Object sender, eventargs E)
{
Dirs (this. tbpath1.text. Trim ());
Dirs (this. tbpath2.text. Trim ());
}

}
}

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.