Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-如何删除internet临时文件_C#_Windows_.net 4.0_Temporary Files - Fatal编程技术网

C#-如何删除internet临时文件

C#-如何删除internet临时文件,c#,windows,.net-4.0,temporary-files,C#,Windows,.net 4.0,Temporary Files,我想完全清除Internet临时文件文件夹。文件夹的位置,例如,C:\Users\Username\AppData\Local\Microsoft\Windows\Temporary Internet Files,取决于Windows的版本,因此它必须是动态的。使用以下路径: 使用此函数Environment.GetFolderPath()和Environment.SpecialFolder枚举获取路径。在遍历该目录时删除该目录中包含的所有文件。您可以这样做 using System.IO;

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

使用以下路径:


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

您可以这样做

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
   }
}

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

在任何情况下,启动的真正基本代码如下:

List<string> someFiles = Directory.EnumerateFiles(Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache)).ToList();
foreach (var fileName in someFiles)
    File.Delete(fileName);
List someFiles=Directory.EnumerateFiles(Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache)).ToList();
foreach(某些文件中的var文件名)
删除(文件名);
当然,你必须考虑访问权限、锁定文件、子文件夹等。


我建议您先从这个开始,然后在实际有一些工作代码时再回答进一步的问题。

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

单击“我的名字”可获得同时删除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();
            }
        }
    }
}

你和尼玛一字不差地复印了吗?如果您从某处复制,您应该提供属性。我不知道这一点,但我从这里获取代码:
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();
            }
        }
    }
}