I want to be able to append text in the middle of a text file instead of at the end. What the below code does it searches for the [BrandIcons1] line, then it should immediately append text directly below [BrandIcons1]. But it appends at the end of the file.

I’m using vb.net 2003. There’s gotta be a way to do this.

Here’s what I have so far:


VB Code:
  1. Private Sub btnWriteToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteToFile.Click
  2.         Dim oFile As System.IO.File
  3.         Dim oRead As System.IO.StreamReader
  4.         Dim oWrite As System.IO.StreamWriter
  5.         Dim FILE_NAME As String = "C:\Program Files\WCS\Mobility\Config\mobility1.ini"
  6.         Dim FILE_COPY As String = "C:\Program Files\WCS\Mobility\Config\mobility2.ini"
  7.         Dim LineIn As String
  8.  
  9.         If oFile.Exists(FILE_NAME) = True Then
  10.             oFile.Copy(FILE_NAME, FILE_COPY, True)
  11.             '
  12.             ' Open and read an exisiting file
  13.             oRead = oFile.OpenText(FILE_NAME)
  14.             While oRead.Peek <> -1
  15.                 LineIn = oRead.ReadLine()
  16.                 If LineIn = "[BrandIcons1]" Then
  17.                     oRead.Close()
  18.  
  19.                     MsgBox(LineIn & " Found.")
  20.                     ' Write to an existing file
  21.                     oWrite = oFile.AppendText(FILE_NAME)
  22.  
  23.                     oWrite.WriteLine("[BrandIcons]")
  24.                     oWrite.WriteLine("BE18=as1.bmp")
  25.                     oWrite.WriteLine("BE01=bu4.bmp")
  26.                     oWrite.WriteLine("BE02=")
  27.                     oWrite.WriteLine("---------")
  28.                     oWrite.Flush()
  29.                     oWrite.Close()
  30.                     Exit While
  31.                 End If
  32.             End While
  33.             MsgBox("Done!")
  34.         Else
  35.             MsgBox("File Does Not Exist: " & vbCrLf _
  36.               & FILE_NAME)
  37.         End If
  38.         Me.Close() 'End the program and close the form
  39.     End Sub

The file to append Mobility1.ini
[MOBILITY]
back drive=d
standard header 1=Digital Matrix, Inc.
standard header 2=Budweiser
standard header 3=Holiday, CA
Last Backup Drive=d
dBev Compatibility=F
Sell Items Not In Product List=T
Auto Lost Placement Enabled=F
Auto Lost Placement Days=365
Use IBS Interface=F
Use IBS For Survey=F
Use IBS For WEARS=F
IBS Application Path=
IBS Window Title=IBS
Expired License Popup Msg Enabled=T

[BrandIcons1]
<append text information here>

[TCOMM]
host name=w55511
host ip=192.168.1.2
host login=wcs
host password=qjris
RAS Entry=Mobility
RAS phone number=555-1212
RAS login=np
RAS password=nkfl
RAS timeout=120
max tcomm number=99
Any help would be appreciated.