Results 1 to 11 of 11

Thread: [Resolved]Files modified by VB are in a locked state

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    [Resolved]Files modified by VB are in a locked state

    I have some code that does a Find & Replace in some text files, and closes the file when complete. However, when I try to use the Kill command in VB to cleanup these files (they are temporary files) when the program is closed, I get errors that the file is in use by another app or process. Is there a way I can force these files to close first? This also happens when I scan a document using the EZTWAIN.dll Scan to native function. Everything goes fine, but if I try to scan again to overwrite the file, the file is in use. This is happening on Windows Vista SP1. Maybe I am missing an important step somewhere, but I don't understand why my app isn't releasing the files. Thanks for any help on this issue.

    -jeremy
    Last edited by hipopony66; Apr 8th, 2009 at 02:01 PM. Reason: Resolved

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Files modified by VB are in a locked state

    Could you show us the code you're using to open, edit, and close the files?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Lively Member
    Join Date
    Mar 2008
    Location
    Charlotte, NC USA
    Posts
    70

    Re: Files modified by VB are in a locked state

    are you working with these files as fileinfo objects? or are you opening a filestream and writing to a file?

    I've run into tons of issues with fileinfo and IO locking. On a polling/ftp application I did a while back i found out that releasing references to a fileinfo object doesn't signal for the locks to release too. In my experiences I had to do a Using Filestream / End Using and then attempt to kill the file.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Files modified by VB are in a locked state

    The issue appears to be with an image I am loading into a Picturebox control with and OpenFileDialog control. It appears the handle to the file is remaining open after image has been loading into the picture box. Any idea how to make VB close the file handle? My code to load the image to the picturebox with a command button is below:

    Code:
     Dim OpenPic As New OpenFileDialog
            Dim strClientPicPath As String
    
    
            With OpenPic
                .CheckFileExists = True
                .ShowReadOnly = False
                .Filter = "JPEG (*.jpg) |*.jpg"
                .FilterIndex = 2
    
                If .ShowDialog = DialogResult.OK Then
    
                    ' Load the specified file into a PictureBox control.
                    strClientPicPath = OpenPic.FileName
                    frmCropImage.pbImageCrop.Image = Image.FromFile(.FileName)
    
                End If
            End With
            Me.Enabled = False
            frmCropImage.Show()
    Thanks for any suggestions.

    -Jeremy

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Files modified by VB are in a locked state

    Image.FromFile does not close the filestream created when opening the image. Use the PictureBox' Load method instead.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Files modified by VB are in a locked state

    Or Image.FromStream

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Files modified by VB are in a locked state

    Which method will allow me to modify the least amount of my existing code? I need to use the OpenFileDialog control for the user to browse to the image, but I need to load it into the picturebox without it remaining locked. All examples I found using the Picturebox.load method referred to loading a URL image.

    Also, I can't simply use:

    Code:
     frmCropImage.pbImageCrop.Image = Image.FromStream(.FileName)
    because the (.Filename) is a string, not system.io.

    Thanks for suggestions.

    -Jeremy

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Files modified by VB are in a locked state

    Have you tried the Load method?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Files modified by VB are in a locked state

    So I need to store the path of the openfiledialog selected file as a bitmap and then paint the bitmap into the picturebox? Is that the easiest method?

    thanks

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Files modified by VB are in a locked state

    No, just use the Load method:

    Code:
    frmCropImage.pbImageCrop.Load(.FileName)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Files modified by VB are in a locked state

    Oh, Ok. That is MUCH easier. Thanks for the help.

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