Delete all files under a path and files under a path

Source: Internet
Author: User

Delete all files under a path and files under a path

To delete all objects under a path:

Delete the file. Insus. NET writes two methods, with the same name and heavy load.

The principle is simple, that is, all directories and subdirectories in the specified path are cyclically deleted. Overload method, with one more parameter. It is to delete the file a few days ago, and determine the creation date of the root file.

The line highlighted by the Code. Insus. NET is used to input a positive number or a negative number regardless of this method. It is eventually processed as a negative number.

Here, you can also restrict the input of 0 values.

Public static void DeleteAllFiles (string physicalPath) {DirectoryInfo di = new DirectoryInfo (physicalPath); foreach (FileInfo fi in di. getFiles () {fi. delete ();} foreach (DirectoryInfo d in di. getDirectories () {DeleteAllFiles (d. fullName) ;}} public static void DeleteAllFiles (string physicalPath, int days) {int day =-(Math. abs (days); DirectoryInfo di = new DirectoryInfo (physicalPath); foreach (FileInfo fi in di. getFiles () {if (fi. creationTime <DateTime. now. addDays (days) fi. delete ();} foreach (DirectoryInfo d in di. getDirectories () {DeleteAllFiles (d. fullName, days) ;}} Source Code



Related Article

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.