none
VB.net - the process cannot access the file because it is being used by another process

    Question

  • I am working on a project that copies an image file from an OpenFileDialog to a specific folder. It seems to work just fine, but when I try to programmatically delete a file from that location, I get an error that says, "the process cannot access the file because it is being used by another process.
    I have a picturebox that shows the image, a listbox that shows the image in the picbox when selected and two buttons ("Add" and "Delete"). I can not figure out why I am getting this error. It seems that I've seen others with issues like this saying it is a 'read only' file when you use 'file.copy'. I don't know a workaround and have not found any in the forums that work. Any ideas.
    Some of the code is written below. Thanks in advanced for any help or advice.
    Getting the file from the OpenFileDialog:
             File.Copy(GetFromPath, CopyToNewPath)
    Copying the file to my location:
            
    Dim strFiles() As String = Directory.GetFiles(Application.StartupPath & "\batch")
            Dim intIndex As Integer = lstImages.SelectedIndex
            Dim SelectedFile As String = strFiles(intIndex)
            File.Delete(SelectedFile)
    Showing the image when selected from the picbox.
    Private Sub lstImages_SelectedIndexChanged(ByVal sender... 
        If lstImages.SelectedIndex > -1
    Then
            
    Dim strDirectory As String = Application.StartupPath &
    "\batch\"
            
    Dim strFiles() As String
    = Directory.GetFiles(strDirectory)
            
    Dim intIndex As Integer
    = lstImages.SelectedIndex
            
    Dim imgImage As
    Image
            
    picImage.Image = New
    System.Drawing.Bitmap(imgImage, FinalWidth, FinalHeight)

    jason
    Monday, August 04, 2008 5:41 AM

Answers

  • I think I figured it out, but had to change a lot of code. every time I opened the image, I was using image.fromfile(OpenFileDialog1.filename) or image.fromfile(ListBox1.selecteditem). These methods were opening the image and I could not find out how to close them so I used the following workaround. I used a filestream instead and disposed the filestream right after I opened the image in a PictureBox. I don't know if there is an easier or quicker way to do this but this will work for now.
    Thanks for the responses.

    Dim FileStream1 As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open, IO.FileAccess.Read)  
    Dim MyImage As Image = Image.FromStream(FileStream1)  
    PictureBox1.Image = MyImage  
     

     


    jason
    • Marked as answer by MU_Jason Friday, August 08, 2008 3:50 PM
    Friday, August 08, 2008 3:49 PM

All replies

  •  Hi MU_Jason,

    I cannot reproduce the problem with the following code in a new project
    But as the error message indicated, there is another process which is accessing to this file. I guess you may access to this file with the first process and forget to release it. I recommend you open the file by "FileShare.None" mode if you still have the problem. If it still happens, you can use the Filemon(http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx) tool or process monitor   ( http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) to see which process are now using this file,and this thread may be give you some help.

    1.Can I know Excel file's status (Busy or Free) from VB.NET?
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/a9365b6c-c207-40f7-9a65-bb33828a6a0b/

    2.Problem with file.delete()
    http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=49960&SiteID=1

    If you have any issue in furture,please reply to me!
    best wish
    xingwei


    east or west ,home is best
    Friday, August 08, 2008 10:33 AM
  • Please also check the Task Manager for any processes accessing this image file.
    Friday, August 08, 2008 10:52 AM
  • I think I figured it out, but had to change a lot of code. every time I opened the image, I was using image.fromfile(OpenFileDialog1.filename) or image.fromfile(ListBox1.selecteditem). These methods were opening the image and I could not find out how to close them so I used the following workaround. I used a filestream instead and disposed the filestream right after I opened the image in a PictureBox. I don't know if there is an easier or quicker way to do this but this will work for now.
    Thanks for the responses.

    Dim FileStream1 As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open, IO.FileAccess.Read)  
    Dim MyImage As Image = Image.FromStream(FileStream1)  
    PictureBox1.Image = MyImage  
     

     


    jason
    • Marked as answer by MU_Jason Friday, August 08, 2008 3:50 PM
    Friday, August 08, 2008 3:49 PM
  • I know this was ages ago but I had exactly the same problem today using VB 2010. Using a filestream to open the image and then closing the filestream wasn't really appropriate for my program though. However, using

    MyImage.dispose

    before

    kill(FileName) or File.Delete(FileName)

    frees up the file so it isn't being "used" anymore.

     

    I hope this might help anyone else having the same problem =^.^=

    I suppose the hardest part is figuring out which variable is causing the problem in the first place though >.<

    • Proposed as answer by Raymai97 Friday, August 03, 2012 12:14 PM
    Wednesday, July 07, 2010 2:31 PM
  • I was having a similar problem unable to delete a displayed picture and changing:
    picBoxShow.Image = System.Drawing.Image.FromFile(fileName)
    to:
    picBoxShow.Load(fileName)
    fixed it.


    Gordon
    • Proposed as answer by Mohammed_Papad Friday, August 03, 2012 11:59 AM
    Wednesday, August 18, 2010 6:50 AM
  • I know this is a long time ago but there have been similar problems more recently. If anyone has a problem like this then force garbage collection using 

    GC.Collect

    either at the end of the load or at the beginning of the save.

    I had this problem when saving text files. It appears that in very simple programs the automatic garbage collection does not kick in in time so the file is still marked as being in use.

    Hope this helps.  

     

    Wednesday, January 12, 2011 2:54 PM
  • Thanks Gordon. I've been struggling with this for hours. I wish I'd thrown in the towel and checked here ours ago, but at least I'm done with the issue now.
    Monday, February 28, 2011 11:58 PM
  • Gordon,

    I have a lot of questions about the demise of DEC. I also realize that asking is intrusive and wonder if I may have some time.

    This would be for personal use. Yike...look at the date on your post.

    Renee


    Not a C officianado
    Tuesday, March 01, 2011 12:49 AM
  • It has to do with another thread having the file open. Use that tak manger to see in close to real time how many threads the process has open.

    Renee


    Not a C officianado
    Tuesday, March 01, 2011 12:52 AM
  • THANKS GORDON YOU ROCK BUDDY IT SOLVED MY PROBLEM TOO.... YOU ARE JUST GREAT
    Friday, August 03, 2012 11:58 AM