当前位置: 移动技术网 > IT编程>开发语言>c# > C#控制IE进程关闭和缓存清理的实现代码

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:class ieutil {    public static void openie(string url) {&nbs
复制代码 代码如下:

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网