Can I delete a file from Internet Cache folder?
Hello
Can I delete a file from Internet Cache folder (Temporary Internet Folder)?
Thanks
Answers
Well I had a few minutes and decided to write up a short example. You should modify this code to delete directories as well, since some sites have sub folders in the cache.
Code SnippetPublic
Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rKey As Microsoft.Win32.RegistryKey Dim path As String'create a key to hold our path
'get the group containing the cache path (read only).
rKey =
My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")'get the path
path = rKey.GetValue(
"Cache")'make sure there's a path before we delete stuff
If path.Length = 0 ThenMsgBox(
"Error reading the path from the registry!") Exit Sub End If'delete all files in the path
For Each file As String In My.Computer.FileSystem.GetFiles(path) My.Computer.FileSystem.DeleteFile(file) NextMsgBox(
"All files have been deleted from " + path) End SubEnd
ClassArgh! The cut and paste from VS2k5 into this message forum is horrid. I lost all of the text highlighting and indention

I corrected most of it, hopefully it won't be too hard to read.
Good Luck,
Johnny
Also,
The BEST way to go about deleting Temp Internet Files, is to use the Win32 API calls that are built into Windows for performing these actions.
The 4 API functions you would use are
FindFirstUrlCacheEntry (There is an Ex version of this method as well)
FindNextUrlCacheEntry (There is an Ex version of this method as well)
DeleteUrlCacheEntry
FindCloseUrlCache
You can look up their declarations on MSDN.
http://msdn2.microsoft.com/en-us/library/aa385473.aspx
I am not saying the other methods won't work. This is just the best way to do it, as these are the same APIs that IE uses when it performs this task itself...
All Replies
Is there a code for that?
Thanks
I've deleted files from my internet cache folder in the past without issues using Windows Explorer. I would image if its OK to do it that way then it should be OK to do it using deleteFile method.
My
.Computer.FileSystem.DeleteFile("C:\Documents and Settings\userid\Local Settings\Temporary Internet Files\junk.html")I'm not sure if there's an object that gives access to IE's tempory files, but I would image there something that will give you path to the files.
Johnny
Perhaps checking the local user cache key to determine the local user Internet file cache location, rather than harcoding something.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Cache
When I delete a file in Temporary Internet Folder, I get error:
Argument cannot be Nothing.
Parameter name: filewhy don't you post the code you are using that causes the error.
It sounds like you are not passing a filename to the delete method (or not a valid filename at least)
kleinma wrote: why don't you post the code you are using that causes the error.
It sounds like you are not passing a filename to the delete method (or not a valid filename at least)
I used the code:
Code SnippetMy.Computer.FileSystem.DeleteFile("C:\Documents and Settings\userid\Local Settings\Temporary Internet Files\junk.html")
John wrote it.
You need to replace the file name at the end of the string argument with a file name that actually exists. So "junk.html" needs to be replaced with a file name that exists. Also the userid needs to be replaced with the logged in user. So "userid" needs to be the named of the logged in user.
i.e. A user named "johnny" is logged in to WindowsXP and there's a file he doesn't like in his cahce named "track.js". The following statement would delete that file.
My.Computer.FileSystem.DeleteFile("C:\Documents and Settings\johnny\Local Settings\Temporary Internet Files\track.js")
Johnny
Perhaps checking the file exists before trying to delete it .
Code SnippetIF My.Computer.Filesystem.FileExists(<FileName>) Then
My.Computer.Filesystem.DeleteFile(<FileName>)
END IF- It says the file doesn't exist although the file does.
Well I had a few minutes and decided to write up a short example. You should modify this code to delete directories as well, since some sites have sub folders in the cache.
Code SnippetPublic
Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rKey As Microsoft.Win32.RegistryKey Dim path As String'create a key to hold our path
'get the group containing the cache path (read only).
rKey =
My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")'get the path
path = rKey.GetValue(
"Cache")'make sure there's a path before we delete stuff
If path.Length = 0 ThenMsgBox(
"Error reading the path from the registry!") Exit Sub End If'delete all files in the path
For Each file As String In My.Computer.FileSystem.GetFiles(path) My.Computer.FileSystem.DeleteFile(file) NextMsgBox(
"All files have been deleted from " + path) End SubEnd
ClassArgh! The cut and paste from VS2k5 into this message forum is horrid. I lost all of the text highlighting and indention

I corrected most of it, hopefully it won't be too hard to read.
Good Luck,
Johnny
John Lieurance wrote: Argh! The cut and paste from VS2k5 into this message forum is horrid. I lost all of the text highlighting and indention
I corrected most of it, hopefully it won't be too hard to read.
Good Luck,
Johnny
Pretty annoying right? Hopefully that will get fixed someday...
good news is copy/paste it back into VS, and it will reformat itself in the IDE...
Also,
The BEST way to go about deleting Temp Internet Files, is to use the Win32 API calls that are built into Windows for performing these actions.
The 4 API functions you would use are
FindFirstUrlCacheEntry (There is an Ex version of this method as well)
FindNextUrlCacheEntry (There is an Ex version of this method as well)
DeleteUrlCacheEntry
FindCloseUrlCache
You can look up their declarations on MSDN.
http://msdn2.microsoft.com/en-us/library/aa385473.aspx
I am not saying the other methods won't work. This is just the best way to do it, as these are the same APIs that IE uses when it performs this task itself...
I always end up cut/paste into notepad - prior to cut/paste into post. This way I keep the indentation although I loose the colorization.
John Lieurance wrote: Well I had a few minutes and decided to write up a short example. You should modify this code to delete directories as well, since some sites have sub folders in the cache.
Code SnippetPublic
Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rKey As Microsoft.Win32.RegistryKey Dim path As String'create a key to hold our path
'get the group containing the cache path (read only).
rKey =
My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")'get the path
path = rKey.GetValue(
"Cache")'make sure there's a path before we delete stuff
If path.Length = 0 ThenMsgBox(
"Error reading the path from the registry!") Exit Sub End If'delete all files in the path
For Each file As String In My.Computer.FileSystem.GetFiles(path) My.Computer.FileSystem.DeleteFile(file) NextMsgBox(
"All files have been deleted from " + path)End Sub
End
ClassThis code doesn't really delete all Temporary Internet Files, it just delete the cache key, so the Internet Files still exist.
I don't mean to be a critic, but your comments ohn1986 are almost useless. You're probably right about the files that are deleted not being the cached files, but you don't bother to mention where the files are. Can you lend us a hand and give us a clue?
Thanks,
Johnny
kleinma wrote: Also,
The BEST way to go about deleting Temp Internet Files, is to use the Win32 API calls that are built into Windows for performing these actions.
The 4 API functions you would use are
FindFirstUrlCacheEntry (There is an Ex version of this method as well)
FindNextUrlCacheEntry (There is an Ex version of this method as well)
DeleteUrlCacheEntry
FindCloseUrlCache
You can look up their declarations on MSDN.
http://msdn2.microsoft.com/en-us/library/aa385473.aspx
I am not saying the other methods won't work. This is just the best way to do it, as these are the same APIs that IE uses when it performs this task itself...
Kleinma,
Can you give us an example on how to call these Win32 API's from .NET?
Thanks,
Johnny
John Lieurance wrote: I don't mean to be a critic, but your comments ohn1986 are almost useless. You're probably right about the files that are deleted not being the cached files, but you don't bother to mention where the files are. Can you lend us a hand and give us a clue?
Thanks,
Johnny
I'm going to tell exactly why I need to delete a file/files in Temporary Internet Folder.
I made a program and button "Check for update" for checking updates.
When someone clicks on "Check for update", the program downloads "update.xml" from my server, then checks & compares values in it.
If I changed values in update.xml, the program won't download the new one, but it will check values in the old one from Temporary Internet Folder.
I hope that you understand me.
How can I fix it?
Thanks
Try doing a search for the update.xml file so you can obtain the path. The missing element here is the path to the internet cache files. If you can find a way to get the path then you can use the routine I posted above.
Johnny

