windows C# - 如何删除 Internet 临时文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6521148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:15:17  来源:igfitidea点击:

C# - How to Delete temporary internet files

c#windows.net-4.0temporary-files

提问by MonsterMMORPG

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.

我想彻底清除 Internet 临时文件夹。文件夹的位置,例如,C:\Users\Username\AppData\Local\Microsoft\Windows\Temporary Internet Files取决于 Windows 的版本,因此它必须是动态的。

采纳答案by Navid Rahmani

use this path: Environment.SpecialFolder.InternetCache

使用此路径: 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
}

回答by Flash

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.

您可能还需要终止 Internet Explorer 进程并更改目录属性,不要认为这对 Index.dat 文件有效,因为 MS 不断更改规则。

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

单击我的名字以获取同时删除 Firefox 文件和 Flash 共享对象的代码

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();
            }
        }
    }
}

回答by ba__friend

Get the path from using this function Environment.GetFolderPath()with Environment.SpecialFolderenumeration. Delete all files contained in that directory while iterating over them.

Environment.GetFolderPath()通过Environment.SpecialFolder枚举使用此函数获取路径。在迭代它们时删除该目录中包含的所有文件。

回答by JAiro

You could do something like this.

你可以做这样的事情。

using System.IO;

public static void Main()
{
 ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))); // Execute ClearFolder() on the IE's cache folder
 }

 void ClearFolder(DirectoryInfo diPath)
 {
   foreach (FileInfo fiCurrFile in diPath.GetFiles())
   {
      fiCurrFile.Delete();
   }
   foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())
   {
      ClearFolder(diSubFolder); // Call recursively for all subfolders
   }
}

回答by slugster

I understand you may not know how to get started, but StackOverflow is not intended to be a place where you can just turn up and request code.

我知道您可能不知道如何开始,但是 StackOverflow 并不是一个您可以直接出现并请求代码的地方。

In any case, a real basic bit of code to get you started would be this:

无论如何,让您入门的真正基本代码如下:

List<string> someFiles = Directory.EnumerateFiles(Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache)).ToList();
foreach (var fileName in someFiles)
    File.Delete(fileName);

Of course you have to consider things like access permissions, locked files, subfolders, etc.

当然,您必须考虑访问权限、锁定文件、子文件夹等内容。

I would suggest you start with this, then come back with further questions when you actually have some working code.

我建议你从这个开始,然后当你真正有一些工作代码时再回来回答更多的问题。