Visual Basic Developer Center > Visual Basic Forums > Visual Basic Language > Can I delete a file from Internet Cache folder?
Ask a questionAsk a question
 

AnswerCan I delete a file from Internet Cache folder?

Answers

  • Tuesday, May 15, 2007 2:41 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Snippet

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'create a key to hold our path

    Dim rKey As Microsoft.Win32.RegistryKey

    Dim path As String

     

    '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 Then

       MsgBox("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)

    Next

     

    MsgBox("All files have been deleted from " + path)

    End Sub

    End Class

     

     

     Argh! The cut and paste from VS2k5 into this message forum is horrid. I lost all of the text highlighting and indention Sad

     I corrected most of it, hopefully it won't be too hard to read.

     

    Good Luck,

    Johnny

  • Tuesday, May 15, 2007 3:11 PMkleinmaMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Monday, May 14, 2007 4:27 PMPenicillin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Is there a code for that?

    Thanks

  • Monday, May 14, 2007 7:05 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Monday, May 14, 2007 7:34 PMspotty Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

     

     

  • Monday, May 14, 2007 7:56 PMPenicillin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    When I delete a file in Temporary Internet Folder, I get error:

    Argument cannot be Nothing.
    Parameter name: file

  • Monday, May 14, 2007 8:42 PMkleinmaMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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)

  • Monday, May 14, 2007 9:29 PMPenicillin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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 Snippet

    My.Computer.FileSystem.DeleteFile("C:\Documents and Settings\userid\Local Settings\Temporary Internet Files\junk.html")

     

    John wrote it.

  • Tuesday, May 15, 2007 1:49 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Tuesday, May 15, 2007 2:32 PMspotty Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Perhaps checking the file exists before trying to delete it .

    Code Snippet

     

    IF My.Computer.Filesystem.FileExists(<FileName>) Then

         My.Computer.Filesystem.DeleteFile(<FileName>)

    END IF

     

  • Tuesday, May 15, 2007 2:39 PMPenicillin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It says the file doesn't exist although the file does.
  • Tuesday, May 15, 2007 2:41 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Snippet

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'create a key to hold our path

    Dim rKey As Microsoft.Win32.RegistryKey

    Dim path As String

     

    '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 Then

       MsgBox("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)

    Next

     

    MsgBox("All files have been deleted from " + path)

    End Sub

    End Class

     

     

     Argh! The cut and paste from VS2k5 into this message forum is horrid. I lost all of the text highlighting and indention Sad

     I corrected most of it, hopefully it won't be too hard to read.

     

    Good Luck,

    Johnny

  • Tuesday, May 15, 2007 3:05 PMkleinmaMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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...

  • Tuesday, May 15, 2007 3:11 PMkleinmaMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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...

  • Tuesday, May 15, 2007 3:59 PMspotty Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

     

  • Wednesday, May 16, 2007 5:25 AMPenicillin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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 Snippet

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'create a key to hold our path

    Dim rKey As Microsoft.Win32.RegistryKey

    Dim path As String

     

    '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 Then

       MsgBox("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)

    Next

     

    MsgBox("All files have been deleted from " + path)

    End Sub

    End Class

     

     

    This code doesn't really delete all Temporary Internet Files, it just delete the cache key, so the Internet Files still exist.

  • Wednesday, May 16, 2007 9:08 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Wednesday, May 16, 2007 9:10 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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

  • Thursday, May 17, 2007 1:57 PMPenicillin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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

  • Thursday, May 17, 2007 2:29 PMJohn C. Lieurance Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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