[2008] Help with cleanup programm
Hi guys :D
I have a problem and it is freakin me out for a week now.
I have made a programm with vb 2008 express that must clean the temp folder the temporary internet folder and the cookies folder.
But somehow it doesn't work,
I think that the problem is that ONE of the many files is used (by another programm) and when the programm comes to that file and it see that it's in use it just stop's cleaning up.
My code is:
Code:
Function clean()
On Error Resume Next
Dim pathCache As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
My.Computer.FileSystem.DeleteDirectory(pathCache, FileIO.DeleteDirectoryOption.DeleteAllContents)
Dim pathCookie As String = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
My.Computer.FileSystem.DeleteDirectory(pathCookie, FileIO.DeleteDirectoryOption.DeleteAllContents)
Dim pathTemp As String = Environment.GetFolderPath(Environment.SpecialFolder.Templates)
My.Computer.FileSystem.DeleteDirectory(pathTemp, FileIO.DeleteDirectoryOption.DeleteAllContents)
Return 0
End Function
And I also have this function I think this is the best one but this one has the same problem I think.
Code:
Public Sub DelIECache()
Dim di As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
On Error Resume Next
If di.Exists = False Then
di.Create()
End If
System.IO.File.SetAttributes(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache).ToString, IO.FileAttributes.Normal)
Dim Cache1 As String
Dim Cache2() As String
Cache2 = IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
For Each Cache1 In Cache2 'Get all files in Temporary internet files, ‘folder and then set their attribute to normal, and then delete them.
IO.File.SetAttributes(Cache1, IO.FileAttributes.Normal)
IO.File.Delete(Cache1)
Next
For Each Cache1 In Cache2 'Get all files in Temporary internet files, ‘folder and then set their attribute to normal, and then delete them.
IO.File.SetAttributes(Cache1, IO.FileAttributes.Normal)
IO.File.Delete(Cache1)
Next
' The true indicates that if subdirectories
' or files are in this directory, they are to be deleted as well.
di.Delete(True)
err: On Error Resume Next'///IGNORE ERROR///
End Sub
I need your help to make it work.
Please edit the code where needed or help me with a solution :thumb:
Already thanks alot :)
Re: [2008] Help with cleanup programm
First up, get rid of that old VB6-style error handling. Your code should be something like this:
vb.net Code:
For Each filePath As String In IO.Directory.GetFiles("folder path here")
Try
IO.File.Delete(filePath)
Catch ex As Exception
'Log error or whatever.
End Try
Next
Because the Try statement is inside the For loop, rather than the other way around, the code will loop back to the next file even if one file fails to delete.
Re: [2008] Help with cleanup programm
I know have this function
Code:
Function clean()
WebBrowser1.Dispose()
Dim pathCache As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
Dim pathCookie As String = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
Dim pathTemp As String = Environment.GetFolderPath(Environment.SpecialFolder.Templates)
For Each filePath As String In IO.Directory.GetFiles(pathCache)
Try
IO.File.Delete(filePath)
Catch ex As Exception
'Log error or whatever.
End Try
Next
For Each filePath As String In IO.Directory.GetFiles(pathCookie)
Try
IO.File.Delete(filePath)
Catch ex As Exception
'Log error or whatever.
End Try
Next
Return 0
End Function
But It still doesn't work for me :ehh:
Re: [2008] Help with cleanup programm
There really isn't much point you copying my comment that says "Log error or whatever" is there? If files aren't deleted then exceptions are being thrown. Don't just ignore them. Examine them to see WHY it's not working.
Re: [2008] Help with cleanup programm
http://img104.imageshack.us/img104/4127/errorsx1.jpg
This is the error that I get, I think that the problem is
that the file index.dat is used by another programm and when the programm comes there, it see's that it generates the error above and then starts over and then does the same thing.
It sort off "get stuck" by the file that is in use.
Re: [2008] Help with cleanup programm
For were you wrote:
Replace it with: (one of these...)
vb Code:
Debug.Print(ex.Message)
Debug.Print(ex.tostring)
I am not sure which one is right...
It is better to Debug.Print instead of creating a msgbox since if this is for a customer/friend etc... they may not understand the error message and it wouldnt be good for someone to be using the program when all of a sudden all these stange codes pop-up...
Quote:
Originally Posted by Customer
...What's all that ****...?
Re: [2008] Help with cleanup programm
ex.Message will give you only the error message, which is doesn't always help very much. I'd suggest that you print out the frames on the call stack, it should help much more. But the ex.ToString may be the best solution, since you'd get all the information there.
Re: [2008] Help with cleanup programm
For testing purposes, until I get rid of any bugs I would do
vb Code:
For Each filePath As String In IO.Directory.GetFiles(pathCookie)
Try
IO.File.Delete(filePath)
Catch ex As Exception
MessageBox.Show("An error occured in Form1; Function Clean() PAth Cookie;Error: " & ex.ToString)
End Try
Tells you where the error is occurring and what the error is. Just my opinion.
Re: [2008] Help with cleanup programm
Ok :)
Didn't know that, i'm kinda new to vb.net
But I have the error now
(see my previous post)
Re: [2008] Help with cleanup programm
You shouldn't be deleting index.dat. That's not a cookie. Cookies are TXT files. You should be getting only the TXT files when you call GetFiles. Read the documentation for the method and it will show you how to filter the files. That said, an error deleting one file shouldn't stop any of the others being deleted.