C#控制IE进程关闭和缓存清理的实现代码

所属分类: 软件编程 / C#教程 阅读数: 47
收藏 0 赞 0 分享
复制代码 代码如下:

class IEUtil {
    public static void openIE(string url) {
        try {
            //System.Diagnostics.Process.Start(url);
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "iexplore.exe";
            p.StartInfo.Arguments = url;
            p.Start();
        } catch(Exception ex) {
            Log.logger("openIE" + url + "----------" + ex.Message);
        }
    }
    public static void closeAllIEProcess() {
        string defaultBrowserName = GetDefaultBrowerName();
        System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName("IEXPLORE");
        foreach(System.Diagnostics.Process proc in procs) {
            proc.Kill();
        }
    }
    public static void CleanCookie() {
        try {
            string[] theFiles = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), "*", System.IO.SearchOption.AllDirectories);
            foreach(string s in theFiles) FileDelete(s);
        } catch(Exception e) {
            Log.logger("Delete cookie error" + e.Message);
        }
    }
    static bool FileDelete(string path) {
        //first set the File's ReadOnly to 0
        //if EXP, restore its Attributes
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        System.IO.FileAttributes att = 0;
        bool attModified = false;
        try {
            //### ATT_GETnSET
            att = file.Attributes;
            file.Attributes &= (~System.IO.FileAttributes.ReadOnly);
            attModified = true;
            file.Delete();
        } catch(Exception e) {
            if (attModified) file.Attributes = att;
            return false;
        }
        return true;
    }
    public static string GetDefaultBrowerName() {
        string mainKey = @"http\shell\open\command";
        string nameKey = @"http\shell\open\ddeexec\Application";
        string strRet = string.Empty;
        try {
            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(nameKey);
            strRet = regKey.GetValue("").ToString();
        } catch {
            strRet = "";
        }
        return strRet;
    }
    /// <summary>
    /// 清除文件夹
    /// </summary>
    /// <param name="path">文件夹路径</param>
    static void FolderClear(string path) {
        System.IO.DirectoryInfo diPath = new System.IO.DirectoryInfo(path);
        if (diPath.Exists) {
            foreach(System.IO.FileInfo fiCurrFile in diPath.GetFiles()) {
                FileDelete(fiCurrFile.FullName);
            }
            foreach(System.IO.DirectoryInfo diSubFolder in diPath.GetDirectories()) {
                FolderClear(diSubFolder.FullName);
                // Call recursively for all subfolders
            }
        }
    }
    /// <summary>
    /// 执行命令行
    /// </summary>
    /// <param name="cmd"></param>
    static void RunCmd(string cmd) {
        ProcessStartInfo p = new ProcessStartInfo();
        p.FileName = "cmd.exe";
        p.Arguments = "/c " + cmd;
        p.WindowStyle = ProcessWindowStyle.Hidden; // Use a hidden window
        Process.Start(p);
    }
    /// <summary>
    /// 删除临时文件
    /// </summary>
    public static void CleanTempFiles() {
        FolderClear(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
        RunCmd("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8");
    }
}
更多精彩内容其他人还在看

C#学习笔记之状态模式详解

这篇文章主要为大家详细介绍了C#学习笔记之状态模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C# 字符串、数组和List的截取和转换实例

下面小编就为大家分享一篇C# 字符串、数组和List的截取和转换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

c#打包文件解压缩的实例

下面小编就为大家分享一篇c#打包文件解压缩的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

下面小编就为大家分享一篇浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题,具有很好的参考价值,希望对大家有所帮助
收藏 0 赞 0 分享

判断一个整数是否是2的N次幂实现方法

下面小编就为大家分享一篇判断一个整数是否是2的N次幂实现方法,实例简洁,具有很好的参考价值。希望对大家有所帮助
收藏 0 赞 0 分享

浅谈c#中config.exe 引发的一些问题

下面小编就为大家分享一篇浅谈c#中config.exe 引发的一些问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于C#开发中的那些编码问题(详谈)

下面小编就为大家分享一篇基于C#开发中的那些编码问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C#实现WPS文件转PDF格式的方法示例

这篇文章主要介绍了C#实现WPS文件转PDF格式的方法,涉及C#针对office组件的相关引用与操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#实现的文件上传下载工具类完整实例【上传文件自动命名】

这篇文章主要介绍了C#实现的文件上传下载工具类,结合完整实例形式分析了C#操作文件上传与下载功能,并且还可针对上传文件自动命名以避免服务器中的文件名重复,需要的朋友可以参考下
收藏 0 赞 0 分享

C# Socket 发送&接收&返回 简单应用实例

下面小编就为大家分享一篇C# Socket 发送&接收&返回 简单应用实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多