Click to See Complete Forum and Search --> : File in use by another process error VB2005
DataMiser
May 5th, 2009, 11:19 AM
I am having an issue where I try to delete a file and get the error message that it is already in use. I can not seem to figure out what is currently using the file. Maybe someone here has an idea.
ImageList2.Images.Clear()
Dim Dq As Integer = 0
For Dq = 0 To DeleteQue.Count - 1
For X = 0 To UBound(TmpAttached)
If DeleteQue.Item(Dq) = TmpAttached(X) Then
Try
'File.Delete(OutPutFolder & "\" & txtAuction.Text & "\" & DeleteQue.Item(Dq))
TmpAttached(X) = ""
TmpThumb(X) = ""
Catch ex As Exception
MsgBox("Unable to remove attached file " & DeleteQue(Dq) & vbCrLf & ex.Message)
End Try
End If
Next
Next
The commented line is where the problem occurs. I added the line which clears the imagelist thinking that may solve the problem but no joy.
The only place I can think of that the file is used is in the image list and the listview. At the point where the code above executes the files where the delete will apply have already been removed from the listview and as far as I can tell should only exist in the image list where they were added using the following code
For X = 0 To UBound(FileNames)
PG.ProgressBar1.Value += 1
myImage = Image.FromFile(OutPutFolder & "\" & txtAuction.Text & "\" & FileNames(X))
ImageList2.Images.Add(myImage)
myImage=nothing
Next
I would have thought that clearing the imagelist would have released the file. Any ideas?
DataMiser
May 5th, 2009, 04:22 PM
Still don't know what the problem is but I needed to get something working to get in the hands of the testers so I created a bit of a workaround that seems to do the job. Rather than delete the files at the commented point in the code above I added code to append the filenames to a txt file. It is not critical that the file be deleted right away so long as it is removed from the system.
I have created another routine that reads this text file and tries to delete each file listed within. If for some reason the program can not delete the file then that filename is written to a different text file which will then overwrite the original txt file after all filenames have been tried. This should result in the new file containing only filenames where the delete failed so that they can be tried again later.
I have also added code in the form load event that fires this sub routine which seems to be working without issue. All references are removed from the data files to these soon to be deleted images but the images themselves and matching thumbnails will not be removed until the next run of the software as it currently stands. I think this will meet the requirements for the customer but I would much rather delete them as we go instead.
So if anyone has some insight on the source of my error message I'm all ears.
dglienna
May 5th, 2009, 06:15 PM
Loop thru BACKWARDS, removing the items that you want. Then they will be correct.
DataMiser
May 5th, 2009, 10:59 PM
Not sure what you mean ? I am already getting the correct items. The delete file function is failing.
Are you saying that if I remove each item from the image list that I will get the desired result whereas it fails when I use clear?
Keep in mind I no longer need any images in the imagelist nor in the listview at this point. They were loaded for the user to visually make selections and the filenames of those selections have been stored in an array, some files will need to be copied and scaled where others will need to be deleted. The actual file delete portion is where the error occurs even after the imagelist has been cleared.
dglienna
May 6th, 2009, 12:31 AM
For that, use
String.Format('File.Delete({0} & "\" & {1} & "\" & {2} ,OutPutFolder,txtAuction.Text,DeleteQue.Item(Dq))
or at least look at the string. Unless your second delete is failing because it's too quick
DataMiser
May 6th, 2009, 08:46 AM
There is no issue with the format of the string as is. The issue is that it says the file is in use and will not allow it to be deleted.
Could be that the delete is happening to fast. I thought about adding a doevents in there but have not did so as of yet.
Perhaps I will try adding a call to my clean up sub at the bottom of that routine and see if it removes the files. If it is a timing issue this may take care of it.
HanneSThEGreaT
May 6th, 2009, 09:43 AM
Isn't there maybe an indexing service, or an antivirus preventing the file from being deleted ¿
Have you considered perhaps doing something like :
Dim FStream As IO.FileStream = Nothing
Dim fiFileI As FileInfo = New FileInfo(OutPutFolder & "\" & txtAuction.Text & "\" & DeleteQue.Item(Dq))
Do
Threading.Thread.Sleep(1000)
Try
FStream = fiFileI.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
Catch ex As IOException
Catch ex As Exception
End Try
If FStream IsNot Nothing Then
FStream.Dispose()
File.Delete(OutPutFolder & "\" & txtAuction.Text & "\" & DeleteQue.Item(Dq))
GC.Collect()
End If
Loop While FStream Is Nothing
¿
It is a bit of a workaround, and a bit more work, but it should help you
DataMiser
May 6th, 2009, 03:15 PM
hmm.. I don't know a delay may do the trick. I will give it a try later. I am 99% sure that what ever process is holding the file open is within my program. The file in question is a preexisting file that I simply loaded into an image list for display purposes and gave the user the option of deleting it. Perhaps it takes longer for the image list to release it than I am allowing for but that is really the only thing I can see that could be an issue. I have indexing service turned off and my virus scanner would have accessed the file when it was loaded rather than when I attemp to delete it.
If I execute the delete function before the image is loaded into the list it works everytime.
I have a couple of things to try now and if all else fails I do have a workaround already in place. Once I get a chance to try some delays I will report back the results.
dglienna
May 6th, 2009, 05:47 PM
Are you setting the IMAGE to Nothing? That'd be my guess...
DataMiser
May 6th, 2009, 11:43 PM
Are you setting the IMAGE to Nothing? That'd be my guess... Yes. You can see in the second block of code in the first post. I create the image object, add it to the imagelist then set the image to nothing and grab the next image and do the same until all images are loaded.
After I am done with the images I clear the list and then try to delete some of the files if the user opted to do so. The filenames of the images that are to be deleted have been added to an arraylist as they made the selections and this is what is used to determine which files need to be deleted. Upon execution of the delete statement the error occurs on any or all of the image files in question. I placed the code in a sub and call it from form load it works fine there.
I have also added in some delay and placed a call to the cleanup sub at the bottom of the routine shown in the first block of code. I have not yet had a chance to test it. Will try to do so tomorrow and see what happens.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.