c# - What is the best way to empty a directory? -


Is there a way to delete all the files & amp; Without the sub-directory of the specified directory without restarting them?

Non elegant solution:

  public static zero empty directory (string path) {if (Directory.Exists (path)) // All files in directory {foreach (var Delete file.GetFiles (Path)) {file.Delete (file); } // Delete all folders foreach (Directory.GetDirectories (Path) Miscellaneous Directory) {Directory.Delete (directory, true); }}}  

How about System.IO.Directory.Delete? There is a recurring option in it, you are also using it. It looks like reviewing your code that you are trying to isolate a bit - emptying the directory without deleting it, right? Okay, you can delete it and make it again :)


In any case, you (or some method you use) all files and subdirectories again Should walk However, you can repeat both files and directories at the same time:

  foreach (System.IO.FileSystemInfo new system in FSI .oo DirectoryInfo (path) .GetFileSystemInfos ()) { If (fsi is system.IO.DirectoryInfo) ((System.IO.DirectoryInfo) fsi). Delete (right); Else fsi.Delete (); }  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -