File in use by another process error. Need help, please.

  • 13 years ago

    Hi.

    I am working on an imaging project, and I've run across a problem I can't seem to solve. Most of my experience is in VB6 and never did any type GDI stuf there.

    Anyway, I have an imagelist control on my main form. I pass it to a routine in a seperate VB module where I populate it like this:

    Dim myDirectoryInfo As DirectoryInfo = New DirectoryInfo(ParsedCommandList("PHOTODIR"))

    For Each myFileInfo As FileInfo In myDirectoryInfo.GetFiles

       Dim CachedFileName As String = My.Application.Info.DirectoryPath & "\cache\" & myFileInfo.Name

       myFileInfo.CopyTo(CachedFileName, True)

       SketchImageList.Images.Add(myFileInfo.Name, Image.FromFile(CachedFileName))

    Next

    Now, if I reload the pictures again, I get a file in use message on this line:

                    myFileInfo.CopyTo(CachedFileName, True)

    I've tried re-instantiating the imagelist control, tried dispose methods. Nothing short of stopping the application and restarting it releases the file. For example:

    For Each image As Image In SketchImageList.Images

    image.Dispose()

    Next

    SketchImageList.Dispose()

    )

    What do I need to do to get my program to release this file?

    Thanks!

  • 13 years ago
    I've run into this before but under different circumstances.  However, I believe the solution is the same.  You'll want to use a filestream instead of using Image.FromFile.  Take a look at this code snippet and try to incorporate a similiar technique into your code.  It should work.
            Dim stream As New IO.FileStream("c:\test.jpg", IO.FileMode.Open)
            ImageList1.Images.Add("test", Image.FromStream(stream))
            stream.Dispose()
  • 13 years ago

    Thanks! This is the anwser I was looking for. I had posted this problem on the official MSDN forums, and got a very similar answer. The main difference was the other solution created a bitmap from the file using the stream object and the used the directcast fuction to place it in the image property. The both the bitmap and the filestream were destroyed.

    I rather like your more obvious solution of using the .FromStream method. And since there is no need to create an extra object ( the bitmap) I like your solution better, as I'm a big fan of effeciency.

  • 13 years ago
    I am getting this same error. I am trying to copy a photo from one location to another with File.Copy(old_Location, new_Location). If the user does this more than once, I get the "File in use by another process" error.... .FromStrream would work for reading from a file....but can you offer a solution for getting around this error while copying an image from location to another? Thanks!

  • 13 years ago

    I don't think File.Copy would cause that error on it's own.  It doesn't lock the file as far as I know.  The only error I was able to get was "file already exists".  Could you post some of your code?  Does your program try to access the new or old file in any other way besides copying?

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger