Question The process cannot access the file because it is being used by another process.

Knvn

Well-known member
Joined
Dec 30, 2009
Messages
45
Programming Experience
Beginner
I have one picturebox in my form which loads a image from CurDir\Photos\imageName when the form gets loaded. I had given a option to change this image through OpenFileDialog.

The code I used to save the new image is:

VB.NET:
 FileCopy(OpenFileDialog1.FileName, globaldata & "\Photos\" & stdrs.Fields(0).Value & ".jpg")

Where,
globaldata=curdir()
stdrs.Fields(0).Value & ".jpg" is the name of the image.

The error I received is:

The process cannot access the file 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\College Management\College Management\bin\Debug\Photos\337CS10001.jpg' because it is being used by another process.

The code I used to load the image(while the form is loading) to the picturebox is:

VB.NET:
PictureBox1.Image = Image.FromFile(globaldata & "\Photos\" & stdrs.Fields(0).Value & ".jpg")

Please help me……
 
I changed my code as,

Loading:
VB.NET:
Try
            fs = New IO.FileStream(globaldata & "\Photos\" & stdrs.Fields(0).Value & ".jpg", IO.FileMode.Open, IO.FileAccess.Read)
            PictureBox1.Image = Image.FromStream(fs)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Inserting the new Image:
VB.NET:
Private Sub InsertPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPhoto.Click
        OpenFileDialog1.FileName = Nothing
        OpenFileDialog1.Filter = "JPEG Format|*.jpg|Bitmap|*.bmp"
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            fs = New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open, IO.FileAccess.Read)
            PictureBox1.Image = Image.FromStream(fs)
        End If
    End Sub

Saving:
VB.NET:
Try

PictureBox1.Image.Dispose()
                    FileCopy(fs.Name, globaldata & "\Photos\" & stdrs.Fields(0).Value & ".jpg")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

But I received the same error message.
Suggestions pls…….
 
Last edited:
You haven't closed the FileStream.
 
(Sorry for the delay, I had exams in last week so I stopped the project for 3-4 days.)

Putting fs.close didn’t work.

To trace the problem more clearly I wrote the following code

VB.NET:
Try
            fs = New IO.FileStream(globaldata & "\Photos\" & strno & ".jpg", IO.FileMode.Open, IO.FileAccess.Read)
            PictureBox1.Image = Image.FromStream(fs)
		
		‘Tried alternatively removing the comments.
            ‘fs.Flush() 
            ‘fs.Dispose()
            fs.Close()

            My.Computer.FileSystem.DeleteFile(globaldata & "\Photos\" & stdrs.Fields(0).Value & ".jpg")
           
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

But get the same error:

The process cannot access the file 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\College Management\College Management\bin\Debug\Photos\337CS10001.jpg' because it is being used by another process.
 
It didn't cause me error. You (or other proces) must be holding another lock to that file. On closer look you have different paths in those statements, how about you use a variable to hold the filepath and use that variable in both statements?
 
You’re right “the lock is hold at the previous form.”
As you told in the previous post(#6), I tried to find out which process is holds the lock on that image and got the solution.

Thank you sir.
 
Back
Top