C#控制IE进程关闭和缓存清理的实现代码
2018-10-12 14:08
851 查看
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");
}
}
您可能感兴趣的文章:
相关文章推荐
- VS C# .NET WebBrowser控件 利用插入JS代码,实现控制弹出IE窗口
- C#实现关闭其他程序窗口或进程代码分享
- C# 清理 IE 缓存文件
- Android设置界面及Preference使用 + 实现开启自启动 清理缓存 杀进程
- c#调用js,以及js调用C#里的函数, c#自己生成js代码,实现对web的控制
- 用C#用C#实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码
- C#清除IE临时文件缓存cookies的方法及核心代码
- UNIX环境高级编程学习之第八章进程控制-用父子进程实现打开kate,返回kate关闭状态
- 用C#用C#实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码
- WinForm+C#代码实现控制应用程序自启动功能
- C#实现启动,关闭与查找进程的方法
- C#对图片进行马赛克处理可控制模糊程度的实现代码
- C#实现手动在IE中增加受信任/受限制站点(更新过后的代码)
- shader学习基础之十一实现纹理的缩放平移和旋转,以及用c#代码合并两种贴图并且控制位置
- C#中点击窗口中的关闭用代码是怎么实现的
- java代码实现开启openoffice服务和关闭sffice.exe进程
- C#简单实现关闭计算机、注销和重启电脑的代码
- C# 全过程用户权限实现策论 (4.过程控制与代码事例说明)
- C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法
- IE,firefox(火狐)浏览器无提示关闭窗口js实现代码小结