Problem: A root folder contains multi-level directories. Multiple subfolders exist at each level, and multiple files exist in each folder. If you want to delete all the sub-files and save the top-level directory folder, how can this problem be solved? Solution 1: directly Delete the root folder and then create a root folder. Void deleteAllFile (string path) {Directory. Delete (path, true); Directory. CreateDirectory (path);} 2. traverse the files in all subdirectories and Delete them one by one. /// <Summary> /// traverse all the files in the folder and subfolders /// </summaryd> /// <c> string dir = ListFiles (new DirectoryInfo (dir )); </c> /// <param name = "fileDirectory"> folder directory path </param> /// <returns> returns the ArrayList object </returns> public static System. collections. arrayList ListFiles (string fileDirectory) {ArrayList arylist = null; if (Directory. exists (fileDirectory) {DirectoryInfo dinfo = new DirectoryInfo (fileDirectory); if (dinfo! = Null) {FileSystemInfo [] files = dinfo. getFileSystemInfos (); for (int I = 0; I <files. length; I ++) {FileInfo file = files [I] as FileInfo; if (file! = Null) {if (arylist = null) arylist = new ArrayList (); arylist. add (file);} else {ListFiles (files [I]. fullName); // for sub-directories, perform recursive call }}} return arylist;} using the first method is the most direct, no recursion, no traversal, but calling the system method, I don't know how the internal principle works. In general, the first method is better than the second method! To be further verified. You can use the following method to delete all files in a folder. Void deleteAllFile (string path) {foreach (string dirStr in Directory. getDirectories (path) {www.2cto.com DirectoryInfo dir = new DirectoryInfo (dirStr);} ArrayList folders = new ArrayList (); FileSystemInfo [] fileArr = dir. getFileSystemInfos (); for (int I = 0; I <folders. count; I ++) {FileInfo f = folders [I] as FileInfo; if (f = null) {DirectoryInfo d = folders [I] as DirectoryInfo; d. delete ();}}}