Question

I have a program, which writes data to a text file in the following format.

test1   -    test1     -     test1     -     test1

After writing the first line, the text fields are cleared to make space for another round of user input. Simply said, this is how it should look:

test1   -    test1     -     test1     -     test1
test2   -    test2     -     test2     -     test2
test3   -    test3     -     test3     -     test3

Here's my code

If Not File.Exists(path) Then
    MessageBox.Show("File doesn't exist in the given path", "No File", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
Else

    Dim reader As New StreamReader(path)
    reader = File.OpenText(path)
    Dim content As String = reader.ReadToEnd
    reader.Dispose()
    reader.Close()

    Dim writer As New StreamWriter(path)  'this is where the exception occurs
    writer.Write("Origin : " & Trim(loadOrigin) & vbTab & "-" & vbTab)
    writer.Write("Destination : " & Trim(destination) & vbTab & vbCrLf & vbCrLf)

    writer.Write(Trim(txtCarrier.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtLocation.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtDest.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtNotes.Text) & vbTab & vbCrLf & vbCrLf)

    writer.Close()
    MessageBox.Show("Text written to file", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
    clearFields()

End If
  1. I used StreamReader to read the content already inside the text file and reach the end of it to append the new line. However, it shows an IOException error with the message The process cannot access the file 'D:\test.txt' because it is being used by another process. It does not help even after I Dispose and/or Close the StreamReader. What am I missing here?

  2. Would this code fulfill my initial purpose of writing multiple lines to the same text file as I have mentioned above? Do I have to make any changes?

Thank you all very much.

Was it helpful?

Solution

reader.Close() should close the file and release all resources for it. Try closing the file prior to calling Dispose (which according to MSDN is not needed because the Close method does that for you.

Dim writer As New StreamWriter(path, True) will open the file for appending text. Dim writer As New StreamWriter(path) or Dim writer As New StreamWriter(path, False) will overwrite the file (if it exists) or create a new file (if it does not exist).

If you are still getting the exception, make sure you don't have the file open elsewhere (like in Notepad).

OTHER TIPS

A very handy VB statement is the Using statement, which can be applied to all resources implementing IDisposable. It ensures that Dispose() will be called in any case before leaving the using block. Even when an exception occurs or when the code block is left by Return for example. (Unless you pull the plug).

Dispose(), in turn, closes the stream.

Dim content As String 
Using reader As StreamReader = File.OpenText(Path)
    content = reader.ReadToEnd
End Using

Using writer As New StreamWriter(Path, True) 'True for append mode
    writer.Write("Origin : " & Trim(loadOrigin) & vbTab & "-" & vbTab)
    writer.Write("Destination : " & Trim(destination) & vbTab & vbCrLf & vbCrLf)

    writer.Write(Trim(txtCarrier.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtLocation.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtDest.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtNotes.Text) & vbTab & vbCrLf & vbCrLf)
End Using
MessageBox.Show("Text written to file", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
clearFields()

Note: Dim reader As New StreamReader(path) is obsolete since you assign a new reader immediately afterwards with reader = File.OpenText(path). Dim reader As StreamReader would be sufficient.

Note also that it is not necessary to read to the end of the file in order to append more text. The value True as second constructor parameter of the StreamWriter does this by itself. Unless you need to read the existing content for another reason, you should drop the StreamReader part completely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top