Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 409
MonsterMMORPG Avatar asked Jun 29 '11 13:06

MonsterMMORPG


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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
}
like image 120
Navid Rahmani Avatar answered Oct 20 '22 12:10

Navid Rahmani


You may also need to kill the process Internet Explore and change the directory attributes and don't go thinking this will work for Index.dat files bacause MS keep changing the rules.

Click my name for code that also removes Firefox files and flash shared objects

using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics; 
using System.Text;

namespace Fidling
{
    public static class SpywareRemoval
    {
        private static void RemoveSpywareFiles(string RootPath, string Path,bool Recursive)
        {
            string FullPath = RootPath + Path + "\\";
            if (Directory.Exists(FullPath))
            {
                DirectoryInfo DInfo = new DirectoryInfo(FullPath);
                FileAttributes Attr = DInfo.Attributes;
                DInfo.Attributes = FileAttributes.Normal;
                foreach (string FileName in Directory.GetFiles(FullPath))
                {
                    RemoveSpywareFile(FileName);
                }
                if (Recursive)
                {
                    foreach (string DirName in Directory.GetDirectories(FullPath))
                    {
                        RemoveSpywareFiles("", DirName, true);
                        try { Directory.Delete(DirName); }catch { }
                    }
                }
                DInfo.Attributes = Attr;
            }
        }

        private static void RemoveSpywareFile(string FileName)
        {
            if (File.Exists(FileName))
            {
                try { File.Delete(FileName); }catch { }//Locked by something and you can forget trying to delete index.dat files this way
            }
        }

        private static void DeleteFireFoxFiles(string FireFoxPath)
        {
            RemoveSpywareFile(FireFoxPath + "cookies.sqlite");
            RemoveSpywareFile(FireFoxPath + "content-prefs.sqlite");
            RemoveSpywareFile(FireFoxPath + "downloads.sqlite");
            RemoveSpywareFile(FireFoxPath + "formhistory.sqlite");
            RemoveSpywareFile(FireFoxPath + "search.sqlite");
            RemoveSpywareFile(FireFoxPath + "signons.sqlite");
            RemoveSpywareFile(FireFoxPath + "search.json");
            RemoveSpywareFile(FireFoxPath + "permissions.sqlite");
        }

        public static void RunCleanup()
        {
            try { KillProcess("iexplore"); }
            catch { }//Need to stop incase they have locked the files we want to delete
            try { KillProcess("FireFox"); }
            catch { }//Need to stop incase they have locked the files we want to delete
            string RootPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToLower().Replace("documents", "");
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Macromedia\Flash Player\#SharedObjects",false);
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\#local", false);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Temporary Internet Files", false);//Not working
            RemoveSpywareFiles("", Environment.GetFolderPath(Environment.SpecialFolder.Cookies), true);
            RemoveSpywareFiles("", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), true);
            RemoveSpywareFiles("", Environment.GetFolderPath(Environment.SpecialFolder.History), true);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Microsoft\Windows\Wer", true);  
            RemoveSpywareFiles(RootPath, @"AppData\Local\Microsoft\Windows\Caches", false);          
            RemoveSpywareFiles(RootPath, @"AppData\Local\Microsoft\WebsiteCache", false);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Temp", false);
            RemoveSpywareFiles(RootPath, @"AppData\LocalLow\Microsoft\CryptnetUrlCache", false);
            RemoveSpywareFiles(RootPath, @"AppData\LocalLow\Apple Computer\QuickTime\downloads", false);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Mozilla\Firefox\Profiles", false);
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Microsoft\Office\Recent", false);
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Adobe\Flash Player\AssetCache", false);
            if (Directory.Exists(RootPath + @"\AppData\Roaming\Mozilla\Firefox\Profiles"))
            {
                string FireFoxPath = RootPath + @"AppData\Roaming\Mozilla\Firefox\Profiles\";
                DeleteFireFoxFiles(FireFoxPath);
                foreach (string SubPath in Directory.GetDirectories(FireFoxPath))
                {
                    DeleteFireFoxFiles(SubPath + "\\");
                }
            }
        }

        private static void KillProcess(string ProcessName)
        {//We ned to kill Internet explorer and Firefox to stop them locking files
            ProcessName = ProcessName.ToLower();
            foreach (Process P in Process.GetProcesses())
            {
                if (P.ProcessName.ToLower().StartsWith(ProcessName))
                    P.Kill();
            }
        }
    }
}
like image 22
Flash Avatar answered Oct 20 '22 11:10

Flash