question

DavidChase-4918 avatar image
0 Votes"
DavidChase-4918 asked davidchase-4554 commented

File in use error

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
dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered davidchase-4554 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I will try this. What is the purpose of the variable FileBackup?

0 Votes 0 ·

Ignore my question on FileBackup.

0 Votes 0 ·
davidchase-4554 avatar image
0 Votes"
davidchase-4554 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered davidchase-4554 commented

Hi @DavidChase-4918,
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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As crazy as this sounds, I was able to get the code from AgaveJoe to work when I used drive letters for the path. For example, it would fail using the following path

\\server\Apps\shareddocs\Photos\C3\2021-08-31 11.11.26.jpg

as the source but was successful using the source below

F:\Apps\shareddocs\Photos\C3\2021-08-31 11.11.26.jpg

What is odd is that my page where the error occurs has not changed since last February and all of a sudden it started to fail. Could there be other reasons that I need to research?

0 Votes 0 ·

@LanHuang-MSFT I do not get an "Accept Answer" option when I view the responses, even though I am logged in.

0 Votes 0 ·