欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-19 09:04:22
复制代码 代码如下: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");
    }
}

上一篇:

下一篇: