-3

Today I'm wondering if I could delete a TEMP file. I have the coding to delete the correct path name but it wont work on any other win pc due to only allowing me to using my folder naming on pc.

This is because when I play a mp3 though resources in winforms it wont let me play another song till TEMP file from previous play is deleted.

I'm looking for all files delete to save me from selecting a path name where I prefer to delete all files in the TEMP as I want to share my tool with others.

Heres the coding I'm using

if (System.IO.File.Exists(@"C:\Users\g\AppData\Local\Temp"))
{
    System.IO.File.Delete(@"C:\Users\g\AppData\Local\Temp");

    MessageBox.Show("TEMP File Deleted");
}
else
{
      MessageBox.Show("Not Done");
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325

3 Answers3

0

You can use Path.GetTempPath() to get the path of the temporary folder, But There may be some files in the temp location are used by another processes, so you should care about the IOExceptions while deleting those files/folders,

// for deleting sub directories in temp
foreach (string subDirectory in Directory.GetDirectories(Path.GetTempPath()))
{
    try
    {
        Directory.Delete(subDirectory, true); 
    }
    catch
    {                       
    }
}
// for deleting files in temp

foreach (string tempfile in Directory.GetFiles(Path.GetTempPath(),"*.*",SearchOption.TopDirectoryOnly))
 {
     try
     {
         System.IO.File.Delete(tempfile);
     }
     catch
     {
     }
 }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • thank you very much this code as worked for me with no issues. thanks for your support in this.... A.R – Anthony Raven Jan 04 '17 at 08:11
  • Anyone interested in this answer should also read through this entire thread for more explanations on the behavior of `Directory.Delete`: https://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true – Parrish Husband Sep 07 '18 at 17:12
0
cls
@echo off
cd %temp%
del %temp%\*.* /f /s /q 
for /D %%f in (%temp%\*) do rmdir "%%f" /s /q

del c:\windows\temp\*.* /f /s /q
for /D %%f in (c:\windows\temp\*) do rmdir "%%f" /s /q

del C:\Windows\Prefetch\*.* /f /s /q
for /D %%f in (C:\Windows\Prefetch\*) do rmdir "%%f" /s /q

del "C:\Users\%username%\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*" /f /s /q
for /D %%f in ("C:\Users\%username%\AppData\Local\Microsoft\Windows\Temporary Internet Files\*") do rmdir "%%f" /s /q

del "C:\Documents and Settings\%username%\Local Settings\Temporary Internet Files\*.*" /f /s /q
for /D %%f in ("C:\Documents and Settings\%username%\Local Settings\Temporary Internet Files\*") do rmdir "%%f" /s /q

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
cls

Save this code in a .bat file ...

It will clear all the temp or dump files from your system.

Milo
  • 3,365
  • 9
  • 30
  • 44
0

for more deep clean use below in ".bat" file

@echo off
del /s /f /q %windir%\temp*.*
rd /s /q %windir%\temp
md %windir%\temp
del /s /f /q %windir%\Prefetch*.*
rd /s /q %windir%\Prefetch
md %windir%\Prefetch
del /s /f /q %windir%\system32\dllcache*.*
rd /s /q %windir%\system32\dllcache
md %windir%\system32\dllcache
del /s /f /q "%SysteDrive%\Temp"*.*
rd /s /q "%SysteDrive%\Temp"
md "%SysteDrive%\Temp"
del /s /f /q %temp%*.*
rd /s /q %temp%
md %temp%
del /s /f /q "%USERPROFILE%\Local Settings\History"*.*
rd /s /q "%USERPROFILE%\Local Settings\History"
md "%USERPROFILE%\Local Settings\History"
del /s /f /q "%USERPROFILE%\Local Settings\Temporary Internet Files"*.*
rd /s /q "%USERPROFILE%\Local Settings\Temporary Internet Files"
md "%USERPROFILE%\Local Settings\Temporary Internet Files"
del /s /f /q "%USERPROFILE%\Local Settings\Temp"*.*
rd /s /q "%USERPROFILE%\Local Settings\Temp"
md "%USERPROFILE%\Local Settings\Temp"
del /s /f /q "%USERPROFILE%\Recent"*.*
rd /s /q "%USERPROFILE%\Recent"
md "%USERPROFILE%\Recent"
del /s /f /q "%USERPROFILE%\Cookies"*.*
rd /s /q "%USERPROFILE%\Cookies"
md "%USERPROFILE%\Cookies"