Click here to Skip to main content
15,917,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to delete all files with specific extension?
FileDelete(path + "*.tempfile") - not helps in this case:(
Posted

Have a look at post #3 on this page:
http://bytes.com/topic/c-sharp/answers/226296-delete-files[^]

What you want in your case:

C#
foreach(string sFile in System.IO.Directory.GetFiles(path, "*.tempfile"))
{
    System.IO.File.Delete(sFile);
}
 
Share this answer
 
You need to get all the files in the directory that match the extension first, then delete each one.

C#
string[] directoryFiles = System.IO.Directory.GetFiles(path, "*.tempfile");
foreach (string directoryFile in directoryFiles)
{
   System.IO.File.Delete(directoryFile);
}
 
Share this answer
 
You will first need to use Directory.GetFiles method to get all the files with given extension. Like this:

string[] filesToDelete = Directory.GetFiles("c:\\test", "*.txt");


Then, if you are using .Net 3.0 or higher, you can use this:

filesToDelete.ToList().ForEach(file => File.Delete(file));


Or loop through the array elements and delete each item one by one. A loop would be better since it would let you handle the cases where you do not have rights to delete a file or if the file is in use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900