To demonstrate how to search and delete files of a specific extension in a particular directory and its sub-directories, let us carry out the following exercise:
Take two TextBoxes and Two Button Controls.
1. txtFolderPath :- this textbox allows user to specify which directory (and its subdirectories) shouls be seached.
2. txtFileExtension :- this textbox is required to give the extension of the file to be searched from the folder specified earlier.
3. btnSearch :- is required to start the search operation
4. btnDelete :- is required to start the delete operation
Then in the Search button code we have to write the following codes :
Example:-
string[] filePaths = Directory.GetFiles(@txtFolderPath.Text,"*." + txtFileExtension.Text, SearchOption.AllDirectories); foreach (string filePath in filePaths) { Response.Write(filePath +"</br>"); }
Then in the Delete button code we have to write the following codes :
Example:–
string[] filePaths = Directory.GetFiles(@txtFolderPath.Text, "*." + txtFileExtension.Text, SearchOption.AllDirectories); foreach(string filePath in filePaths) { File.Delete(filePath); }