Link to home
Start Free TrialLog in
Avatar of chuckalicious
chuckaliciousFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Clearing IE Cache using VB.NET

Deleting Internet Explorer Cache using VB.Net

I need to be able to clear the IE cache using VB.Net.

I currently have a simple app that does a few things with a new IE window, created using:

CreateObject("InternetExplorer.Application")

Any help greatly appreciated
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Avatar of jimstar
jimstar

Here's some code that will get the History folder via the .NET SpecialFolders, and then get files within that for deletion. Essentially, you want to get rid of everything within that special folder. Some may not be accessible if they are in use though.

private void frmMain_Load(object sender, System.EventArgs e)
{
path = Environment.GetFolderPath(Environment.SpecialFolder.History) + "\\";
getList(path);

}

private void getList(string thepath)
{
DirectoryInfo dir = new DirectoryInfo(thepath);
DirectoryInfo[] dirs = dir.GetDirectories();
FileInfo[] files = dir.GetFiles();
foreach(DirectoryInfo d in dirs)
{
getList(d.FullName);
}

foreach(FileInfo f in files)
{
lstHistory.Items.Add(f.FullName);
}
}

private void clrHist()
{
for(int i = 0; i < lstHistory.Items.Count; i++)
{
try
{
File.Delete(lstHistory.Items[i].Text);
}
catch(IOException e)
{
MessageBox.Show("Error: \n\n\n" + e.Message);
}
}
}
Avatar of chuckalicious

ASKER

jimstar,

I've pasted that code into MS VB 2005 Basic Edition and it's just throwing up lots of errors about end of statements exptected and delcations expected. Should this code work in 2005 Basic Edition?

Note I'm very new to VB so the above might be a stupid question!
Oops, sorry! That is C# code. I'll see if I can convert it over, though it's been a while since I've done VB.NET. :)
I found an article that gives VB.NET code for clearing the IE cache, written by Microsoft Support. Give this a try:

http://support.microsoft.com/kb/262110/EN-US/

THanks JimStar

When I paste that code in, it tells me that VB no longer supports Type and I should use Structure instead and that the variables aren't declared.
So I go from this:
Private Type INTERNET_CACHE_ENTRY_INFO
   dwStructSize As Long
    szRestOfData(1024) As Long
End Type

to this:

Private Structure INTERNET_CACHE_ENTRY_INFO
    Dim dwStructSize As Long
    Dim szRestOfData(1024) As Long
 End Structure

I then get the error of "Arrays declared as structure members cannot be declared with an initial size" for the line Dim szRestOfData(1024) As Long

I have no idea what this means :(

JimStar, the code you posted in C# seems a lot more straight forward than the code in the link you provided, which is erroring with something about an unbalanced stack.....

Would it be possible for you to convert it from C# to vb.net?

Thanks
It was actually an error on my part - I converted it already, but the code didn't work. Deleting the files is not possible because explorer.exe has them open all the time.

The only option appears to be using the functions that are specifically designed for clearing the cache.

I'm working on a solution right now. The problem is that older versions of VB use Long, and newer versions need to use Integer to pass information around. There are a few issues with the code on Microsoft's site that I am trying to correct.
ASKER CERTIFIED SOLUTION
Avatar of jimstar
jimstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Depending on your specific project, you may need to rename Sub Main() to a different name that you call when you want to clear the cache.

Let me know if you have any trouble.
Thanks. A few things with that. According to my VB Studio, instead of Imports System.Runtime.InteropServices we should be using Imports System.Runtime.InteropServices.ComTypes.FILETIME

and also it wants the variables Marshal and DllImport declared. What should I declare them as?

Thanks again
Marshal and DllImport are under System.Runtime.InteropServices, which is why it gets imported. When you changed it to ... ComTypes.FILETIME, it made it so that the Marchal and DllImport couldn't be found.

What specifically is the error you get when you just use:
Imports System.Runtime.InteropServices
?
One option that should fix any issue with Marshal and DllImport would be to prepend the System.Runtime.InteropServices string to them.

So, wherever you see:
Marshal

Make it:
System.Runtime.InteropServices.Marshal

... and the same with DllImport.

It's odd that you're getting issues with the general import, but the above tip should fix any problem.
Ok no errors, now, the last question :) How do I call this? My code is being run via a button on a form.

Thanks
To call it from a form, you'll want to rename Sub Main() in Module1 to something like Sub ClearCacheNow(). Then, you should be able to double click on the button on your form, and then type ClearCacheNow() in the button's subroutine.
Alright, one last question, does this clear just the cache, or does it clear cookies and completed form information as well?
This code clears everything.
I should clarify - it clears "Temporary Internet Files", which contains cached files and cookie information. I don't believe it includes the autocomplete data, typed URLs, or history.