Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please i am trying to write the result of a loop in my code into a text file that on the harddisk. I am getting an IOEXECPTION error that the file you are trying to access is currently used by another process.

Sub Main()
Using myreader As StreamReader = New StreamReader("C:\Users\Jamiebones\Desktop\learning_application\learning_application\Phone Number.txt")

While Not (myreader.EndOfStream)


Dim line As String = myreader.ReadLine
Dim strBuilder As New Text.StringBuilder
If String.IsNullOrEmpty(line) = False AndAlso line.Length > 1 Then
strBuilder.Append("234")
strBuilder.Append(line.Remove(0, 1))
End If
Console.WriteLine(strBuilder.ToString)

If strBuilder.Length > 0 = True Then
Dim writer As StreamWriter = New StreamWriter("c:/phone numbers.txt")
writer.Write(strBuilder.ToString)

End If
writer.closestream()

End While
Console.ReadLine()

End Using
The line of code that throws this error is "writer.Write(strBuilder.ToString)"
Please i need help on this.Thanks
Posted

1 solution

First the writer.Close (not CloseStream!) should be inside the If block directly above it, not after the End If.

Second, the path seperator character in Windows is "\", not "/".
 
Share this answer
 

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