Posts

Showing posts from June, 2016

Recursively delete files from Directory C#.NET

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= newDirectoryInfo(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=newDirectoryInfo(directoryPath); List<FileInfo> fileList=DirInfo.GetFiles("*",SearchOption.AllDirectories).ToList(); foreach(FileInfofile