We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

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

top

C#

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login