我正在从数据库中获取值,并尝试使用 UrlHistoryWrapper 类将它们插入到 Internet Explorer 历史记录中。当我将for
条件设置为数千时,我会收到此错误,但是,当我将 for 条件设置为小于 100 时,它会运行良好。我检查了内存使用情况,应该没有问题。如果 chrome.URLs 和 firefox.URLs 有数千个值,我怎样才能使这段代码能够运行?
这是我的代码:
UrlHistoryWrapperClass urlHistory;
UrlHistoryWrapperClass.STATURLEnumerator enumerator;
List<STATURL> list = new List<STATURL>();
urlHistory = new UrlHistoryWrapperClass();
enumerator = urlHistory.GetEnumerator();
bool display = false;
chrome.GetHistory(display);
for (int i = 0; i < chrome.URLs.Count; i++ )
{
URL url = chrome.URLs[i];
urlHistory.AddHistoryEntry(url.url, url.title, ADDURL_FLAG.ADDURL_ADDTOHISTORYANDCACHE);
}
Firefox firefox = new Firefox();
firefox.GetHistory(display:false);
foreach (URL url in firefox.URLs)
{
MessageBox.Show(url.url);
urlHistory.AddHistoryEntry(url.url, url.title, ADDURL_FLAG.ADDURL_ADDTOHISTORYANDCACHE);
}
这是我的错误:
** * **异常文本** * **** System.Runtime.InteropServices.COMException (0x80070008):没有足够的存储空间来处理此命令。(HRESULT 异常:0x80070008)在 UrlHistoryLibrary.UrlHistoryWrapperClass.AddHistoryEntry(String pocsUrl,String pocsTitle,ADDURL_FLAG dwIE(String pocsUrl,String pocsTitle,ADDURL_FLAG dwFlags)在 Namespace.IE)中的 UrlHistoryLibrary.IUrlHistoryStg2.AddUrl(String pocsUrl,String pocsTitle,ADDURL_FLAG dwFlags)。 \Users\Chris\documents\visual studio 2010\Projects\TestURLGUI4\TestURLGUI4\URLHistory.cs:C:\Users\Chris\documents\visual studio 2010\Projects\TestURLGUI4\TestURLGUI4 中 Namespace.Program.SyncAll() 的第 270 行\URLHistory.cs:TestURLGUI2.Form1.button11_Click(Object sender, EventArgs e) 中的第 152 行:\Users\Chris\documents\visual studio 2010\Projects\TestURLGUI4\TestURLGUI4\Form1.cs:System.Windows 中的第 247 行.
Edit:
Here is my new code:
for (var i = 0; i < firefox.URLs.Count; i++ )
{
URL url = firefox.URLs[i];
urlHistory.AddHistoryEntry(url.url, url.title, ADDURL_FLAG.ADDURL_ADDTOHISTORYANDCACHE);
Thread.Sleep(3);
form.label1.Text = i + "/" + firefox.URLs.Count;
// MessageBox.Show(url.url);
Application.DoEvents();
}
Now, with this code, I can go up to 8447 without getting this error, but then it gets thrown.
I had someone else try to do this, and this is what he said:
Just tried your code and and it seems that there is a limit on the number of the url records that can be added. I did the following steps.
- In Internet Explorer, Clear the history of websites I've visited.
- I tried your code.
- Got a COMException (0x80070008): Not enough storage is available to process this command. The exception is thrown at record number 41021 for me (Windows 7, 64bit).
- I tried your code again, and Got a very same exception. This time, No record have be added. To add the url records again. I have to clear the history of websites in Internet Explorer. I could not find a way to fix the problem.
For anyone who needs to know, the source code for the URLhistoryWrapper can be found on this CodeProject project.