We help IT Professionals succeed at work.

FileSystem.MoveFile - System.IO.IOException: The process cannot access the file because it is being used by another process.

Hello All;

I have a file move issue that only started because I had to take a sub-folder on my site, and [convert to Application]

Here is a portion of the code I am using.
            For Each postedFile As HttpPostedFile In filMyFile.PostedFiles
                Dim getfile As String = System.IO.Path.GetFileName(postedFile.FileName)
                Dim FileToMove As String
                Dim MoveLocation As String

                           GlobalVariables.savepath = Context.Server.MapPath("../Media/Temp/")
                           GlobalVariables.MovePath = Context.Server.MapPath("../Media/")


                            FileToMove = (GlobalVariables.savepath & getfile & ".mp3")
                            MoveLocation = (GlobalVariables.MovePath & strPath & "/" & getfile & ".mp3")


                            If System.IO.File.Exists(FileToMove) = True Then
                                My.Computer.FileSystem.MoveFile(FileToMove, MoveLocation) ' <<<< ERROR POINTS HERE
                            End If

Open in new window


The code was working correctly until I changed the sub-folder to [convert to Application]
Once I did that, I received this error.

Permissions are correctly given for the folder, so it does not seem to be a permissions issue.
I used Windows Resource Monitor.
Chose the CPU tab.
Searched for mp3 in the [Associated Handles]
Here is one line from the results of the search


Image                PID      Type      Handle Name
w3wp.exe      12272      File             G:\InetPub\wwwroot\mysite\Media\Temp\03112020223702438.mp3

I read that I needed to close the opened file.
I tried to do.
mp3.Dispose() ' did nothing
filMyFile.Dispose() ' did nothing

Open in new window


Any other suggestions to help get past this issue, so it will go back to working correctly?
Thanks.

Wayne
Comment
Watch Question

Author, Web Developer
CERTIFIED EXPERT
Top Expert 2009
Commented:
Figured it out.
Dim reader As New Mp3FileReader(theFilePath)
Dim getDuration As String = reader.TotalTime.ToString("mm\:ss")

I added the below right after the code above, and it worked.

                reader.Close()
                reader.Dispose()