CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    File in use by another process error VB2005

    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.

    Code:
    
    ImageList2.Images.Clear()
    Dim Dq AsInteger = 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)
             EndTry
          EndIf
       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
    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?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: File in use by another process error VB2005

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: File in use by another process error VB2005

    Loop thru BACKWARDS, removing the items that you want. Then they will be correct.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: File in use by another process error VB2005

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: File in use by another process error VB2005

    For that, use

    Code:
    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: File in use by another process error VB2005

    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.
    Last edited by DataMiser; May 6th, 2009 at 08:49 AM.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: File in use by another process error VB2005

    Isn't there maybe an indexing service, or an antivirus preventing the file from being deleted ¿
    Have you considered perhaps doing something like :
    Code:
    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

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: File in use by another process error VB2005

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: File in use by another process error VB2005

    Are you setting the IMAGE to Nothing? That'd be my guess...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: File in use by another process error VB2005

    Quote Originally Posted by dglienna View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured