Search code examples
c#windows.net-4.0temporary-files

C# - How to Delete temporary internet files


I want to clear the temporary Internet files folder completely. The location of the folder, e.g., C:\Users\Username\AppData\Local\Microsoft\Windows\Temporary Internet Files, depends on the version of Windows, so it has to be dynamic.


Solution

  • use this path: Environment.SpecialFolder.InternetCache

    string path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
    //for deleting files
    System.IO.DirectoryInfo di = new DirectoryInfo(path);
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete(); 
    }
    foreach (DirectoryInfo dir in di.GetDirectories())
    {
        dir.Delete(true); //delete subdirectories and files
    }