Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to insert lines of text into an existing text file?

How to insert lines of text into an existing text file?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MJay
    wrote on last edited by
    #1

    Currently i am using the below coding to create a text file and write something on it.. Dim wfile As New System.IO.StreamWriter("c:\test.txt") wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You") wfile.Close() The text file content should be like this Hello Are You What if i want to insert "How" into the text file to modify the text file become like this.. Hello How Are You Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks..

    N D 2 Replies Last reply
    0
    • M MJay

      Currently i am using the below coding to create a text file and write something on it.. Dim wfile As New System.IO.StreamWriter("c:\test.txt") wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You") wfile.Close() The text file content should be like this Hello Are You What if i want to insert "How" into the text file to modify the text file become like this.. Hello How Are You Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks..

      N Offline
      N Offline
      Nadroj
      wrote on last edited by
      #2

      it can be done. i dont know how, tho.. :(( i do know how to append text though.. that is, to add text to the end of a text file. i dont know how to go about inserting text onto the specific line/location into an existing file, sry. however if the appending text method is suitable, i can explain it. ------------------------ Jordan. III

      M 1 Reply Last reply
      0
      • N Nadroj

        it can be done. i dont know how, tho.. :(( i do know how to append text though.. that is, to add text to the end of a text file. i dont know how to go about inserting text onto the specific line/location into an existing file, sry. however if the appending text method is suitable, i can explain it. ------------------------ Jordan. III

        M Offline
        M Offline
        MJay
        wrote on last edited by
        #3

        thanks for replying me.... can u tell me ur method how to append text as well? thanks for help..

        D 1 Reply Last reply
        0
        • M MJay

          Currently i am using the below coding to create a text file and write something on it.. Dim wfile As New System.IO.StreamWriter("c:\test.txt") wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You") wfile.Close() The text file content should be like this Hello Are You What if i want to insert "How" into the text file to modify the text file become like this.. Hello How Are You Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks..

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You can't "insert" text without rewriting the file from the point where you are inserting. There are a couple of ways to do it: 1.) Seek to the point where you want to insert text, read the text from that point to the end of the file and store it. Seek back to the point where you want to insert text and write what you wanted to insert, then write everything you read in and stored previously. Done... 2.) Create a second file (text2.txt). Read everything from your original file (text1.txt), up to the point where you want to insert, and write it out to text2.txt. Write out what you want to insert. Then finished reading text1.txt and write everything out to text2.txt. Then delete text1.txt and rename text2.txt to text1.txt. Done... RageInTheMachine9532

          1 Reply Last reply
          0
          • M MJay

            thanks for replying me.... can u tell me ur method how to append text as well? thanks for help..

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Open the file for Append and start writing.

            Dim myFile As New FileStream( "filename.txt", FileMode.Append )
            myFile.WriteLine( "Text I want to append..." )
            myFile.Close()

            RageInTheMachine9532

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups