C#控制IE进程关闭和缓存清理的实现代码_C#教程-查字典教程网
C#控制IE进程关闭和缓存清理的实现代码
C#控制IE进程关闭和缓存清理的实现代码
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:classIEUtil{publicstaticvoidopenIE(stringurl){try{//System.Di...

复制代码 代码如下:

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 = @"httpshellopencommand";

string nameKey = @"httpshellopenddeexecApplication";

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#教程学习
    编程开发子分类