Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7405205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:22:08+00:00 2026-05-29T05:22:08+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:22:08+00:00Added an answer on May 29, 2026 at 5:22 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a small program which logs text data to a file on
I have a program which reads data from 2 text files and then save
I have written a program which writes a list of data to a '.dat'
I have a spring batch program which reads from a file and writes to
I have a simple spring batch program which reads data from a INPUT file
I have created a program which takes a text file full of 3 letter
I have a program (written in C#) that reads/writes its data directly (direct file
I have a program which runs an external, command line utility and reads the
I have written a program in Java to read in a text file of
I have a NetCDF file, which contains data representing total precipitation across the globe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.