我有一种在 Internet Explorer 中打开 URL 的方法。如果已经打开了 Internet Explorer,它应该在新选项卡中打开。如果没有,它应该打开一个新的 Internet Explorer。
我的代码:
public static void OpenURL(string Url)
{
bool already_navigated = false;
ShellWindows instances = new ShellWindows();
//Check if there is an Internet Explorer
if (instances.Count > 0)
{
foreach (InternetExplorer ie in instances)
{
if (ie.Name == "Windows Internet Explorer")
{
if (!already_navigated)
{
//Navigate and open in New Tab
already_navigated = true;
ie.Navigate(Url, 0x10000);
//Bring window to front
IntPtr hwnd = (IntPtr)ie.HWND;
WindowHandler.Window w = new WindowHandler.Window(hwnd, "Internet Explorer");
w.Minimize();
w.Restore();
}
}
}
}
//No internet explorer found!
if (!already_navigated)
{
//Start new Internet Explorer
Process proc = Process.Start("IExplore.exe", Url);
}
}
这很好用!但是当我调用这段代码时,我的内存会无限循环增加,直到我得到一个OutOfMemoryException ...
经过反复试验,我发现这段代码引发了异常:
public static void OpenURL(string Url)
{
bool already_navigated = false;
ShellWindows instances = new ShellWindows();
/*
//Check if there is an Internet Explorer
if (instances.Count > 0)
{
foreach (InternetExplorer ie in instances)
{
if (ie.Name == "Windows Internet Explorer")
{
if (!already_navigated)
{
//Navigate and open in New Tab
already_navigated = true;
ie.Navigate(Url, 0x10000);
//Bring window to front
IntPtr hwnd = (IntPtr)ie.HWND;
WindowHandler.Window w = new WindowHandler.Window(hwnd, "Internet Explorer");
w.Minimize();
w.Restore();
}
}
}
}
//No internet explorer found!
if (!already_navigated)
{
//Start new Internet Explorer
Process proc = Process.Start("IExplore.exe", Url);
}*/
}
这段代码没有:
public static void OpenURL(string Url)
{
bool already_navigated = false;
/*
ShellWindows instances = new ShellWindows();
//Check if there is an Internet Explorer
if (instances.Count > 0)
{
foreach (InternetExplorer ie in instances)
{
if (ie.Name == "Windows Internet Explorer")
{
if (!already_navigated)
{
//Navigate and open in New Tab
already_navigated = true;
ie.Navigate(Url, 0x10000);
//Bring window to front
IntPtr hwnd = (IntPtr)ie.HWND;
WindowHandler.Window w = new WindowHandler.Window(hwnd, "Internet Explorer");
w.Minimize();
w.Restore();
}
}
}
}*/
//No internet explorer found!
if (!already_navigated)
{
//Start new Internet Explorer
Process proc = Process.Start("IExplore.exe", Url);
}
}
这给我留下了一个结论,问题在于:
ShellWindows instances = new ShellWindows();
但经过一番谷歌搜索,我找不到任何有类似问题的人。所以我不确定我是否做错了什么,或者我对这个问题是否正确。
有谁知道发生了什么?