Recursively delete files from given directory keeping the directory structure intact C#.NET To get all the files present in the subdirectories of the given directory, use "SearchOption.AllDirectories" DirectoryInfo DirInfo = new DirectoryInfo ( directoryPath ); DirInfo . GetFiles ( "*" , SearchOption . AllDirectories ). ToList (). ForEach ( file => file . Delete ()); The above code will delete all the files for the given directory while keeping the structure of directory intact. To send files in recycle bin instead of deleting it from system use "FileSystem.DeleteFile" function of VisualBasic assembly. Add reference to VisualBasic dll and add "using Microsoft.VisualBasic.FileIO" DirInfo = new DirectoryInfo ( directoryPath ); List < FileInfo > fileList = DirInfo . GetFiles ( "*" , SearchOption . AllDirectories ). ToList (); foreach ( FileInfo file in fileList
Learn Tips and tricks, Concepts, Ideas for software development