Click here to Skip to main content
15,350,730 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I execute the following code in a loop that changes the file being read each time. The code reads the input file, scrubs the markup coding, and writes it to a temp file for use later. It works the first time it is called, but on the second call in the loop, I get a "used by another process" error for the file I am writing to on the following line:
"xWrite = My.Computer.FileSystem.OpenTextFileWriter(pFileNameNew, False)"

Public Sub ScrubPAC(pacfile As String)
        Dim xRead As System.IO.StreamReader
        Dim xWrite As System.IO.StreamWriter
        Dim sBuf As String
        Dim sTemp As String
        Dim sFileName As String
        Dim pFileNameNew As String

        'Scrub PAC
        currentdir = "C:\SOM Folder\VB Development\SOMBBStatGen\"

        sFileName = pacfile
        pFileNameNew = currentdir & "pbp\WIPPAC.prt"

        'read original pac file
        sTemp = ""
        xRead = IO.File.OpenText(sFileName)
        Do Until xRead.EndOfStream
            sBuf = xRead.ReadLine
            sTemp = sTemp & sBuf & vbCrLf

        'remove print codes
        sTemp = Replace(sTemp, "[1]", "")
        sTemp = Replace(sTemp, "[2]", "")

        'Write Clean PAC
        xWrite = My.Computer.FileSystem.OpenTextFileWriter(pFileNameNew, False)

    End Sub

I've been struggling with this for hours, any ideas?

1 solution

Disregard, I solved it, of course right after I posted the question. I didn't close the file after reading it in another SUB.
Dev O'Connor 17-Sep-15 17:09pm
Did the same thing before, i posted a queston claiming a split of an array was calculating wrong, only to realise i solved it before i posted it and posted a working solution! doh!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900