Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. File in use error

File in use error

Scheduled Pinned Locked Moved Visual Basic
helpquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jon_Boy

    Are there any other methods in your app using the file? If so, you need to cleanly end those resources so that the file is unlocked. make sure the file is not readonly. Make sure you don't have other apps using the file.

    Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

    T Offline
    T Offline
    TheComputerMan
    wrote on last edited by
    #3

    Thanks for responding. No nothing else is using the file. It just gets placed in the picture box:

                    Try
                        Me.PictureBox1.BackgroundImage = GetAvatar(ContactID)
                        Me.cmdDeleteImage.Enabled = True
                    Catch ex As Exception
                        Me.cmdDeleteImage.Enabled = False
                    End Try
    

    Where GetAvatar =

    Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Image
        Dim pict As Drawing.Image
        'This just collects a file name
        Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
    
        Try
    
            If strFilename.Trim > "" Then
                pict = Drawing.Image.FromFile(strFileName)
            Else
                pict = My.Resources.nouserpic\_grey
            End If
    
        Catch ex As Exception
    
            pict = My.Resources.nouserpic\_grey
    
        End Try
        Return pict
    End Function
    

    Might it be because it ic alled in from another routine?? :confused:

    J C 2 Replies Last reply
    0
    • T TheComputerMan

      Thanks for responding. No nothing else is using the file. It just gets placed in the picture box:

                      Try
                          Me.PictureBox1.BackgroundImage = GetAvatar(ContactID)
                          Me.cmdDeleteImage.Enabled = True
                      Catch ex As Exception
                          Me.cmdDeleteImage.Enabled = False
                      End Try
      

      Where GetAvatar =

      Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Image
          Dim pict As Drawing.Image
          'This just collects a file name
          Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
      
          Try
      
              If strFilename.Trim > "" Then
                  pict = Drawing.Image.FromFile(strFileName)
              Else
                  pict = My.Resources.nouserpic\_grey
              End If
      
          Catch ex As Exception
      
              pict = My.Resources.nouserpic\_grey
      
          End Try
          Return pict
      End Function
      

      Might it be because it ic alled in from another routine?? :confused:

      J Offline
      J Offline
      Jon_Boy
      wrote on last edited by
      #4

      Well, if you rem out the logic in GetAvatar and loading it into the picture box, can you delete the file? :)

      Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

      T 1 Reply Last reply
      0
      • T TheComputerMan

        Thanks for responding. No nothing else is using the file. It just gets placed in the picture box:

                        Try
                            Me.PictureBox1.BackgroundImage = GetAvatar(ContactID)
                            Me.cmdDeleteImage.Enabled = True
                        Catch ex As Exception
                            Me.cmdDeleteImage.Enabled = False
                        End Try
        

        Where GetAvatar =

        Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Image
            Dim pict As Drawing.Image
            'This just collects a file name
            Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
        
            Try
        
                If strFilename.Trim > "" Then
                    pict = Drawing.Image.FromFile(strFileName)
                Else
                    pict = My.Resources.nouserpic\_grey
                End If
        
            Catch ex As Exception
        
                pict = My.Resources.nouserpic\_grey
        
            End Try
            Return pict
        End Function
        

        Might it be because it ic alled in from another routine?? :confused:

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #5

        TheComputerMan wrote:

        pict = Drawing.Image.FromFile(strFileName)

        This locks the file. You need instead to create a new bitmap. open the file, copy the bitmap, and delete the original. At least, that's how I've got around this bug in GDI+ in the past.

        Christian Graus Driven to the arms of OSX by Vista.

        T 1 Reply Last reply
        0
        • T TheComputerMan

          This is the code that calls the form where the file is displayed in a picture box. frmCEA.ContactID = intContactID frmCEA.Text = "Edit a contact" frmCEA.ShowDialog() strAvatarToDelete = frmCEA.AvatarForDeletion frmCEA.Close() frmCEA.Dispose() If strAvatarToDelete.Trim > "" Then My.Computer.FileSystem.DeleteFile(strAvatarToDelete) End If I have tried on the form, and off the form like this, to delete an avatar but every time I get this message - even if I want to change photos. The process cannot access the file 'C:\Documents and Settings\All Users\Application Data\.....\Avatars\AV000001' because it is being used by another process. Any out there know why this might be??:confused:

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #6

          This has been widely documented since the early days of .NET 1.0. The problem is you loaded the image file directly into an Image object in the PictureBox control. The Image object keeps the file locked for it's entire lifetime. The solution is to load the image using a FileStream, then pass the resulting stream to the construcotr of a new Bitmap object, then close the stream, and assign the Bitmap object to the Image property of the PictureBox.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          T 2 Replies Last reply
          0
          • C Christian Graus

            TheComputerMan wrote:

            pict = Drawing.Image.FromFile(strFileName)

            This locks the file. You need instead to create a new bitmap. open the file, copy the bitmap, and delete the original. At least, that's how I've got around this bug in GDI+ in the past.

            Christian Graus Driven to the arms of OSX by Vista.

            T Offline
            T Offline
            TheComputerMan
            wrote on last edited by
            #7

            I tried that Christian (using pict2 = pict.clone and then disposing pict) but still the same problem. It obviously carries the info accross to the clone.

            Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap
                Dim pict As Drawing.Bitmap
                Dim pict2 As Drawing.Bitmap
                'This just gets the file name
                Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
            
                Try
            
                    If strFilename.Trim > "" Then
                        pict = Drawing.Bitmap.FromFile(strFilename)
                        pict2 = pict.Clone()
                        pict.Dispose()
                    Else
                        pict2 = My.Resources.nouserpic\_grey
                    End If
            
                Catch ex As Exception
            
                    pict2 = My.Resources.nouserpic\_grey
            
                End Try
                Return pict2
            End Function
            

            Looks like I might have to try storing the pic in the database, but I did not want to do that as it is only an SQL Server 2005 Compact so space limited. Thanks anyway! (If you can think of any other way I would like to hear from you)

            L 1 Reply Last reply
            0
            • J Jon_Boy

              Well, if you rem out the logic in GetAvatar and loading it into the picture box, can you delete the file? :)

              Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

              T Offline
              T Offline
              TheComputerMan
              wrote on last edited by
              #8

              Thanks for the suggestion Jon_Boy but unfortunately that would impact my code. I will give it a go if all else fails however.

              J 1 Reply Last reply
              0
              • D Dave Kreskowiak

                This has been widely documented since the early days of .NET 1.0. The problem is you loaded the image file directly into an Image object in the PictureBox control. The Image object keeps the file locked for it's entire lifetime. The solution is to load the image using a FileStream, then pass the resulting stream to the construcotr of a new Bitmap object, then close the stream, and assign the Bitmap object to the Image property of the PictureBox.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                T Offline
                T Offline
                TheComputerMan
                wrote on last edited by
                #9

                Thanks Dave. I think I know how to do that so I will give that a go.

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  This has been widely documented since the early days of .NET 1.0. The problem is you loaded the image file directly into an Image object in the PictureBox control. The Image object keeps the file locked for it's entire lifetime. The solution is to load the image using a FileStream, then pass the resulting stream to the construcotr of a new Bitmap object, then close the stream, and assign the Bitmap object to the Image property of the PictureBox.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  T Offline
                  T Offline
                  TheComputerMan
                  wrote on last edited by
                  #10

                  Thanks a million Dave. It worked a treat! :) :) :) No need to reply to this. Just posting the code in case it helps someone else.

                  Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap
                      Dim pict As Drawing.Bitmap
                      'This just gets the file name
                      Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
                  
                      Try
                  
                          If strFilename.Trim > "" Then
                              Dim fs As New System.IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read)
                              pict = Drawing.Bitmap.FromStream(fs)
                              fs.Close()
                          Else
                              pict = My.Resources.nouserpic\_grey
                          End If
                  
                      Catch ex As Exception
                  
                          pict = My.Resources.nouserpic\_grey
                  
                      End Try
                      Return pict
                  End Function
                  

                  Code above corrected: :) Thanks Fabio

                  modified on Saturday, January 3, 2009 10:20 AM

                  F 1 Reply Last reply
                  0
                  • T TheComputerMan

                    I tried that Christian (using pict2 = pict.clone and then disposing pict) but still the same problem. It obviously carries the info accross to the clone.

                    Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap
                        Dim pict As Drawing.Bitmap
                        Dim pict2 As Drawing.Bitmap
                        'This just gets the file name
                        Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
                    
                        Try
                    
                            If strFilename.Trim > "" Then
                                pict = Drawing.Bitmap.FromFile(strFilename)
                                pict2 = pict.Clone()
                                pict.Dispose()
                            Else
                                pict2 = My.Resources.nouserpic\_grey
                            End If
                    
                        Catch ex As Exception
                    
                            pict2 = My.Resources.nouserpic\_grey
                    
                        End Try
                        Return pict2
                    End Function
                    

                    Looks like I might have to try storing the pic in the database, but I did not want to do that as it is only an SQL Server 2005 Compact so space limited. Thanks anyway! (If you can think of any other way I would like to hear from you)

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #11

                    TheComputerMan wrote:

                    pict2 = pict.Clone()

                    Christian is right, but you did not take his advise.

                    TheComputerMan wrote:

                    Bitmap.FromFile(strFilename)

                    This is what keeps your file locked. Hence make and use a copy of the IMAGE and dispose of the original right away.

                    TheComputerMan wrote:

                    any other way

                    No need. The one way is fine if executed properly. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Love, happiness and fewer bugs for 2009!


                    1 Reply Last reply
                    0
                    • T TheComputerMan

                      Thanks for the suggestion Jon_Boy but unfortunately that would impact my code. I will give it a go if all else fails however.

                      J Offline
                      J Offline
                      Jon_Boy
                      wrote on last edited by
                      #12

                      Wasn't suggesting that as a perm. fix - just as a way for you now and in the future easily determine if and where your code was locking a file.

                      Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

                      1 Reply Last reply
                      0
                      • T TheComputerMan

                        Thanks a million Dave. It worked a treat! :) :) :) No need to reply to this. Just posting the code in case it helps someone else.

                        Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap
                            Dim pict As Drawing.Bitmap
                            'This just gets the file name
                            Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
                        
                            Try
                        
                                If strFilename.Trim > "" Then
                                    Dim fs As New System.IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read)
                                    pict = Drawing.Bitmap.FromStream(fs)
                                    fs.Close()
                                Else
                                    pict = My.Resources.nouserpic\_grey
                                End If
                        
                            Catch ex As Exception
                        
                                pict = My.Resources.nouserpic\_grey
                        
                            End Try
                            Return pict
                        End Function
                        

                        Code above corrected: :) Thanks Fabio

                        modified on Saturday, January 3, 2009 10:20 AM

                        F Offline
                        F Offline
                        Fabio V Silva
                        wrote on last edited by
                        #13

                        Why do you need picbyteArray variable?! You're not using it...

                        T 1 Reply Last reply
                        0
                        • F Fabio V Silva

                          Why do you need picbyteArray variable?! You're not using it...

                          T Offline
                          T Offline
                          TheComputerMan
                          wrote on last edited by
                          #14

                          Hi Fabio, Thanks for that. I had pulled the code from another program where I am using it to store the picture in SQL Server and forgot to take that bit out! Duh!! :rolleyes: The corrected code block is:

                          Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap
                              Dim pict As Drawing.Bitmap
                              'This just gets the file name
                              Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID)
                          
                              Try
                          
                                  If strFilename.Trim > "" Then
                                      Dim fs As New System.IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read)
                                      pict = Drawing.Bitmap.FromStream(fs)
                                      fs.Close()
                                  Else
                                      pict = My.Resources.nouserpic\_grey
                                  End If
                          
                              Catch ex As Exception
                          
                                  pict = My.Resources.nouserpic\_grey
                          
                              End Try
                              Return pict
                          End Function
                          

                          Sorry if that caused any confusion to anyone.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups