Group,

I'm using System.IO.File.Move to move an existing file into another folder. However it the program is stopping at this point with the error message "The process cannot access this file because is being used by another process". Unfortunately I can't find "the other process". For the record, I've also tried System.IO.File.Copy which works fine. However when I try to delete the original file, I get the same message.

Here's the full code I'm using:

                    Dim objReader As New System.IO.StreamReader(getRestranName(i))
                    fileName = getRestranName(i)
                    If System.IO.File.Exists(fileName) Then
                        Do While objReader.Peek() <> -1
                            txtLine = objReader.ReadLine()
                            prpName = Trim(Microsoft.VisualBasic.Left(txtLine, 30))
                            destinationFile = destinationFolder & "\" & Trim(Microsoft.VisualBasic.Right(fileName, 15))
                            If prpName = propertyName Then
                                System.IO.File.Move(fileName, destinationFile)
                            End If
                            Exit Do
                        Loop
                    End If

Is this issued happening because I have "fileName" still open as "objReader"? If this is the case, is there a way to close it while still in my loop?

As always, thanks for your help.

Don

Is this issued happening because I have "fileName" still open as "objReader"?

Yes.

If this is the case, is there a way to close it while still in my loop?

Well, you could dispose of the reader and break out of the loop, but that's very ugly and prone to error. And I suspect the reason you're asking is you want to continue reading from the reader after moving the file. I'd be more inclined to tweak the logic so that this exclusive access problem simply doesn't exist.

It looks to me you want something like this:

Dim objReader As New System.IO.StreamReader(getRestranName(i))
fileName = getRestranName(i)
If System.IO.File.Exists(fileName) Then
    Do While objReader.Peek() <> -1
        txtLine = objReader.ReadLine()
        prpName = Trim(Microsoft.VisualBasic.Left(txtLine, 30))
        destinationFile = destinationFolder & "\" & Trim(Microsoft.VisualBasic.Right(fileName, 15))
        If prpName = propertyName Then
            objReader.Close()
            System.IO.File.Move(fileName, destinationFile)
            Exit Do                
        End If
    Loop
End If

Duh!! I knew that.... why I couldn't think of it, I chalk up to "olds-heimers....

Here's what I ended up doing:

If lineNo = 3 And programName = "(res.restran)" Then
      restranSave = "C:\Restran Conversion\Restran\" & propertyNo & "restran.txt"
      System.IO.File.Copy(fileName, restranSave, True)
      objReader.Dispose()
      System.IO.File.Delete(fileName)
      Call ConvertRestran()
      Call ConvertToPDF()
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.