Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How actually i can delete the temporary file using C# ?. I already get the list of temporary file using (System.IO.Path.GetTempPath) and the code is shown below. So how do i need to delete the file using C# ?

What I have tried:

if(checkBox2.Checked){
    DirectoryInfo Dir = new DirectoryInfo(System.IO.Path.GetTempPath());

    FileInfo[] Files = Dir.GetFiles();

    foreach (FileInfo file in Files)
    {
        listView1.Items.Add(file.Name);
        File.Delete(file.Name);
    }
}
Posted
Updated 5-Aug-17 19:51pm
v2

C#
<pre> 
static void Main(string[] args)
        {
            List<string> listView1 = new List<string>();
            DirectoryInfo Dir = new DirectoryInfo(System.IO.Path.GetTempPath());

            FileInfo[] Files = Dir.GetFiles();
            int faildelcount = 0;

            foreach (FileInfo file in Files)
            {
                try
                {
                    File.Delete(file.Name);
                }
                catch (Exception ex)
                {
                    faildelcount++;
                }
            }

            Console.WriteLine("Temp files cleared successfully");
            if(faildelcount > 0)
            Console.WriteLine("Unable to delete {0} files", faildelcount.ToString());
            Console.ReadLine();
            
        }
 
Share this answer
 
v2
Try:
File.Delete(file.Name);
 
Share this answer
 
Comments
Asyraf Patt 6-Aug-17 1:54am    
if(checkBox2.Checked){
DirectoryInfo Dir = new DirectoryInfo(System.IO.Path.GetTempPath());

FileInfo[] Files = Dir.GetFiles();

foreach (FileInfo file in Files)
{
listView1.Items.Add(file.Name);
File.Delete(file.Name);
}
}


why the file in C:\Users\Administrator\AppData\Local\Temp still not delete ?
Thomas Daniels 6-Aug-17 2:02am    
Probably because GetTempPath() does _not_ return the folder you mention in your comment... On my computer, it returns C:\Windows\Temp and it's not unlikely that it is on yours too - check this documentation to find out how GetTempPath's location is decided.
OriginalGriff 6-Aug-17 2:06am    
As ProgramFox suggests, it may be the path.
It also may be that the temporary file is in use by a different application.
In both cases, did you get any exceptions, and what does the debugger show when you run that code line by line?
Michael_Davies 6-Aug-17 2:35am    
There's more than one Temp directory, System.IO.Path.GetTempPath() returns the first temp directory it finds in a search list;

This method checks for the existence of environment variables in the following order and uses the first path found:

The path specified by the TMP environment variable.
The path specified by the TEMP environment variable.
The path specified by the USERPROFILE environment variable.
The Windows directory.

The Temp variable is set to the system's temp directory, usually C:\Windows\Temp and you need access rights before you can even list the contents.

There are other temps for the user in the <User Profile Dir>\AppData folder in Local and Roaming sub directories. It might be better to check them for a Temp directory and then delete from them, the only thing that would prevent a file being deleted there would be that it was open by another process.

To gain access to the Windows\Temp see https://msdn.microsoft.com/en-us/library/c1f66bc2(v=vs.110).aspx
BillWoodruff 6-Aug-17 7:11am    
imho this comment deserves being posted as a solution, and being voted up !
string tempfolder = ("C:\\Users\\Administrator\\AppData\\Local\\Temp");
          string[] tempfiles = Directory.GetFiles(tempfolder, "*.*", SearchOption.AllDirectories);


          foreach (string filePath in Directory.GetFiles(tempfolder, "*.*", SearchOption.AllDirectories))
          {
              try
              {
                  FileInfo currentFile = new FileInfo(filePath);
                  currentFile.Delete();

              }
              catch (Exception ex)
              {
                  Debug.WriteLine("Error on file: {0}\r\n   {1}", filePath, ex.Message);
              }

          }
 
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