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