Software Technology Tips

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);

}


Related Tags:

C#, Wildcard, Search, Delete

Author: Smruti Sahoo

C#

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login