问题描述
我编写了以下代码来关闭所有IE(Internet Explorer)进程。
I wrote the codes below to close all IE (Internet Explorer) Processes.
private void Kill_IE()
{
Process[] ps = Process.GetProcessesByName("IEXPLORE");
foreach (Process p in ps)
{
p.Kill();
}
}
问题是一些打开的IE窗口显示警告:
The problem is some opened IE windows show an alert :
alert("do not close my web site"); //in JavaScript
或
confirm_box("do you really want to exit?" , "YES|NO"); //in JavaScript
退出前( Unload_Form )我的程序可以'关闭那些窗口。
如何忽略这些警报并强制关闭这些窗口?
before exit(Unload_Form) and my program can't close those windows.
How can i ignore those alerts and force close those windows?
编辑:
以下方法无法通过恼人的提醒关闭该浏览器...
Edit :
The ways below can't close that browser with that annoying alert...
显示黑色控制台:
Process.Start("taskkill", "/im iexplore.exe /f /t");
隐藏黑色控制台:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "taskkill.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/f /t /im iexplore.exe";
Process.Start(startInfo);
任何想法?
推荐答案
我已经测试过这段代码,每次都适用于我。我正在使用IE11并打开了生成警报的远程和本地站点,并且进程仍然关闭而没有错误。
I've tested this code and it works every time for me. I'm using IE11 and have opened both remote and local sites that generate alerts and the processes still close without an error.
我建议将此过程包装在try / catch中看看是否有任何异常产生,因为我认为错误在其他地方。
I suggest wrapping this process in a try/catch to see if there any exceptions generated as I believe the fault lies elsewhere.
private void Kill_IE()
{
Process[] ps = Process.GetProcessesByName("IEXPLORE");
foreach (Process p in ps)
{
try
{
if (!p.HasExited)
{
p.Kill();
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Unable to kill process {0}, exception: {1}", p.ToString(), ex.ToString()));
}
}
}
这篇关于如何强制关闭所有IE进程 - 使用form_unload上的alert()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!