Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, please help.
If I have in my current app folder some files like f1.txt,f2.txt, f3.txt and I want to delete all those files ending with .txt, how can I do this?
I know about the File.Delete method, but how can i use it here?
Posted

You can use Directory.GetFiles for filtering and getting all the files and then you can delete by using File.Delete.

sample from MSDN,

http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx?PHPSESSID=tn8k5...[^]
 
Share this answer
 
Something like this:

using System.IO;

string[] files = Directory.GetFiles(myPath, "*.txt", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
    File.Delete(file);
}
 
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