Editing a text file

Hi Experts,

All I want is some simple code that will open an existing text document ("C:\Text.txt") and add 3 lines of code into the text document at line 2;
1)Export=,
2)CEPX,
3)c=test.asc

Thanks Alot,

Tom


LVL 3
TommyTwoPintsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Olaf_RabbachinCommented:
Hi TommyTwoPints,

try this:

   Public Sub InsertText(ByVal strTextFile_FullPath As String, ByVal strInsertText As String, ByVal intAfterLine As Integer)
      Try
         Dim sr As New System.IO.StreamReader(strTextFile_FullPath)
         Dim sb As New System.Text.StringBuilder
         Dim intCurrentLine As Integer = 0

         'Read in all lines until hitting the one we want to insert after
         While sr.Peek > 0 AndAlso intCurrentLine < intAfterLine
            sb.Append(sr.ReadLine & ControlChars.NewLine)
            intCurrentLine += 1
         End While

         'Now insert the text that has been passed
         sb.Append(strInsertText & ControlChars.NewLine)

         'Add whatever's left in the text-file
         sb.Append(sr.ReadToEnd)

         'Dispose of the stream-reader
         sr.Close()

         'In order to write back, we'll need to overwrite the file, so simply delete it here
         IO.File.Delete(strTextFile_FullPath)

         'Create a stream-writer for writing back to the file
         Dim sw As New System.IO.StreamWriter(strTextFile_FullPath)
         sw.Write(sb.ToString)

         'Dispose of the stream-writer - this will also flush the file to disk
         sw.Close()

      Catch ex As Exception
         MessageBox.Show(ex.ToString)
      End Try
   End Sub

Cheers & HTH,
Olaf

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi TommyTwoPints;
Hope this meets your needs.

Dim fileText As String = "C:\Temp\Text.txt"      ' File to edit
Dim fileTemp As String = "C:\Temp\Temp.txt"  ' Temp file write new info to
Dim sr As New StreamReader(fileText)             ' Open the input file
Dim sw As New StreamWriter(fileTemp)            ' Open the temp file
Dim input As String                                        ' Holds input files read info
Dim index As Integer = 1                                ' Used to fine line 2

input = sr.ReadLine()                                     ' Read a line of input
While Not input = Nothing
    If index = 2 Then                                      ' If at line 2 add info
    sw.Write("Export=," & NewLine & "CEPX," & NewLine & "c=test.asc" & NewLine)
    End If
    sw.WriteLine(input)                                   ' Write data to output file
    input = sr.ReadLine()                                ' Get the next line of data
    index += 1                                              ' Add 1 to line number
End While

sr.Close()                                                   ' Close input file
sw.Close()                                                  ' Close output file

File.Delete(fileText)                                     ' Delete original file
File.Move(fileTemp, fileText)                        ' Rename Temp.txt to Text.txt

Good luck
Fernando
Fernando SotoRetiredCommented:
Sorry make sure you add the following imports.

Imports System.IO
Imports System.Environment
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.