File in use error

David Chase 681 Reputation points
2021-09-28T17:50:38.78+00:00

Getting error message below but cannot figure out why.

"The process cannot access the file '\Server\Photos\C4\2021-09-28 10.48.37.jpg' because it is being used by another process"

We are reading a file from one directory, shrinking the file size and uploading it, then deleting it from the original location. The error is happening on the line (see code below) that does a File.Delete(strFilePath). I have tried it without the "using" clause but I get the same thing.

                    Dim filePath As String = strFolderPath & "\" & strFileName
                    Dim filePathB As String = strBackupPath & "\" & strFileName
                    If File.Exists(filePath) = False Then
                        'If Image is less that 800KB then move it
                        'otherwise stream it to a smaller file.
                        If fi.Length < 800000 Then
                            fi.CopyTo(filePathB)
                            fi.MoveTo(filePath)
                        Else
                            Dim bmpImg As Bitmap = Nothing
                            Using fileStream As FileStream = File.OpenRead(strFilePath)
                                Dim memStream As New MemoryStream()
                                memStream.SetLength(fileStream.Length)
                                fileStream.Read(memStream.GetBuffer(), 0, CInt(fileStream.Length))
                                bmpImg = FilesClass.Resize_Image(fileStream, 2533, 1900)
                                bmpImg.Save(filePath, ImageFormat.Jpeg)
                                bmpImg.Save(filePathB, ImageFormat.Jpeg)
                                fileStream.Close()
                                fileStream.Dispose()
                                File.Delete(strFilePath)
                            End Using
                        End If

                    End If
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,248 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2021-09-28T20:14:19.657+00:00

    You have a lot of unknown variables and methods which make a code review close to impossible.

    What stands out to me is the code reads a FileStream into a MemoryStream buffer but does nothing with the MemoryStream. There's also a "Using" block but you're disposing the file stream in the block???

    I created a basic example below that will hopefully help with the logic.

        Sub Main()
            Dim FileSource As String = "C:\Images\Source\strat.jpg"
            Dim FileDestination As String = "C:\Images\Destination\strat.jpg"
            Dim FileBackUp = "C:\Images\Backup\strat.jpg"
    
            Dim fi As IO.FileInfo = New IO.FileInfo(FileSource)
    
            If Not IO.File.Exists(FileDestination) Then
                'If Image is less that 800KB then move it
                'otherwise stream it to a smaller file.
                If fi.Length < 800000 Then
                    fi.CopyTo(FileBackUp)
                    fi.MoveTo(FileDestination)
                Else
                    Using fileStream As IO.FileStream = IO.File.OpenRead(FileSource)
                        Dim original = New Drawing.Bitmap(fileStream)
                        Dim resized = New Drawing.Bitmap(original, New Drawing.Size(2533, 1900))
                        resized.Save(FileDestination, Drawing.Imaging.ImageFormat.Jpeg)
                        resized.Save(FileBackUp, Drawing.Imaging.ImageFormat.Jpeg)
                    End Using
                    IO.File.Delete(FileSource)
                End If
            End If
        End Sub
    

  2. david chase 41 Reputation points
    2021-09-28T21:43:47.86+00:00

    Tried your code and got the exact same error at the line IO.File.Delete(FileSource)

    0 comments No comments

  3. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2021-09-29T09:27:53.233+00:00

    Hi @David Chase ,
    First you confirm whether your process is the only one accessing the file,
    If it is the only one, you have two options: rewrite your code to serialize file access (not always feasible and not always wanted) or apply a retry mode.
    If you open the file in another part of the program, you must check whether it is closed properly after each use.

    For specifics, you can refer to the article:
    https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Lan Huang