process could not access the file...

clweeks

Member
Joined
Feb 21, 2007
Messages
18
Programming Experience
3-5
Hi, I have what must be a simple problem and I just need an extra pair of eyes. I'm looping through each FileInfo in a DirectoryInfo.GetFiles and then moving them to a new location. The app moves all of them but the last, at which point it barfs out a "process could not access the file <filename> because it is being used by another process." Worse yet, this code is snipped from a console app that I wrote a while back that works fine.

VB.NET:
    Private Function fileMover(ByVal sourcePath As String, ByVal destPath As String, ByVal delete As Boolean) As Boolean
        'If the source path ends with a "\" then we're instantiating a directoryInfo and moving all the files
        'in the directory (not recursing down), else we're instantiating a fileInfo and just moving that file.
        Dim fileGrabber As New FileInfo(sourcePath)
        Dim dirGrabber As New DirectoryInfo(sourcePath)
        If sourcePath.Substring(sourcePath.Length - 1, 1) = "\" Then Return fileCopy(dirGrabber, destPath, delete) Else Return fileCopy(fileGrabber, destPath, 1, delete)
    End Function

    Private Function fileCopy(ByVal source As DirectoryInfo, ByVal dest As String, ByVal delete As Boolean)
        If Not Directory.Exists(dest) Then Directory.CreateDirectory(dest)
        For Each file As FileInfo In source.GetFiles
            If Not fileCopy(file, dest & file.Name, 1, delete) Then Return False
        Next
        Return True
    End Function

    Private Function fileCopy(ByVal source As FileInfo, ByVal dest As String, ByVal depth As Int16, ByVal delete As Boolean)
        If depth = 5 Then Return False
        If dest.Substring(dest.Length - 1, 1) = "\" Then dest &= source.Name
        If Not Directory.Exists(dest) Then Directory.CreateDirectory(dest)
        If source.CopyTo(dest).Length = source.Length Then
            If delete = True Then source.Delete()
            Return True
        Else
            fileCopy(source, dest, depth + 1, delete)
            Return False
        End If
    End Function

So, I expect I'm having a brain-fart, who wants to point it out to me? Also, I've swapped out the mechanics of how it's copying (source.moveto, etc) and it's still just the same error. The fact that only the final file (irrespective of what that file actually is) is a problem is bothering me.
 
OK, so it's just not the code that I posted above. That code, extracted into test applications always works. But I am still confused about what's happening.

Those file move subs are called from a form event right after another sub that does some GDI+ stuff as well as text logging. I think the error is coming from there -- some part of it is keeping the final image file open which prevents deletion. I've commented out all the code that I can while still testing the core function and it comes to this:
VB.NET:
        For Each tiffFile As FileInfo In root.GetFiles("*.tif")
            tiff = Image.FromFile(tiffFile.FullName)
            Dim tempBM As New Bitmap(tiff.Width, tiff.Height, PixelFormat.Format64bppArgb) '<---here
            tiff = convertToG4tiff(tempBM) '<---here
            tiff.Save(outputDirectory & "\" & tiffFile.Name, tifCodec, enParams)
            tiff.Dispose()
        Next

Those two lines that I marked with "<---here" are significant because if we remove them, the file-copy operation later works just fine. When they're there, we get the "process could not access the file..." exception on only the last file of the batch when trying to delete the originals after the copy has succeeded.

Also, the convertToG4tiff(BitMap) function can be seen here. (Posted by my coworker.) From having tried all these things in all these combinations, it seems like that function is causing the issue, but it doesn't seem like it ought to be capable of it since it doesn't handle the filesystem.

I still expect that I'm missing something dumb, but I'm sure not finding it and would _really_ appreciate any input if you know what's going on.
 
Try between those "here"s to call tiff.Dispose(), because now you are just reassigning the 'tiff' variable reference to a new image, the previous one loaded from file is left for GC to collect maybe hours later. Calling Dispose on this explicitly should release the file handle.
 
Back
Top