Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
delete the temp files in C#.net i want to list all the temp files in the listbox and then delete all the files
Posted

Hi,

To get Temp path on your system use this code:

C#
var tmpPath = Path.GetTempPath();


To get all files in that directory use:

C#
var files = Directory.GetFiles(tmpPath, "*.*", SearchOption.AllDirectories);


To add files to your ListBox:
C#
listBox1.AddRange(files);


Deleting files is easy:
C#
foreach(var file in files)
{
    if (File.Exists(file))
    {
        File.Delete(file);
    }
}


And don't forget to add System.IO to your using clause at top of the source file.

More informations here:
http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles%28v=vs.110%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.file.exists%28v=vs.90%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.90%29.aspx[^]

Cheers!
 
Share this answer
 
 
Share this answer
 
v2
 
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