By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,593 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,593 IT Pros & Developers. It's quick & easy.

Append into file

100+
P: 111
Hi

I want to append one text from textbox1 only at beginning and textbox2 at the end of the file. How do i Do?
Dec 18 '07 #1
Share this Question
Share on Google+
4 Replies


lotus18
100+
P: 866
Hi

I want to append one text from textbox1 only at beginning and textbox2 at the end of the file. How do i Do?
What do you mean by file? As a filename? or a as a file itself? What version of vb are you using?

Rey Sean
Dec 18 '07 #2

100+
P: 111
I am working in VB.Net

The file type is .txt
Dec 18 '07 #3

debasisdas
Expert 5K+
P: 8,127
Append means to add to the end of the file with existing data. Since your requirement is different.

1. Read the content of the entire file.
2. Concatenate the text1 at the beginning and text2 at the end and then
3. Overwrite the file.
Dec 18 '07 #4

100+
P: 111
I did like this

Expand|Select|Wrap|Line Numbers
  1.         Dim FILE_NAME As String
  2.         Dim objFile As System.IO.File
  3.         Dim objRead As System.IO.StreamReader
  4.         'Dim objWrite As System.IO.StreamWriter
  5.         'Dim intFileNum As Integer
  6.  
  7.         FILE_NAME = My.Application.Info.DirectoryPath & "\test.txt"
  8.         Dim LineAll As New System.Text.StringBuilder(CInt(New System.IO.FileInfo(FILE_NAME).Length + txtBefore.Text.Length + txtAfter.Text.Length))
  9.  
  10.         ' intFileNum = FreeFile()
  11.  
  12.         If objFile.Exists(FILE_NAME) Then
  13.             objRead = objFile.OpenText(FILE_NAME)
  14.             If txtBefore.Text <> String.Empty Then
  15.                 LineAll.Append(txtBefore.Text & vbNewLine)
  16.             End If
  17.             While objRead.Peek <> -1
  18.                 LineAll.Append(objRead.ReadLine() & vbNewLine)
  19.             End While
  20.             If txtAfter.Text <> String.Empty Then
  21.                 LineAll.Append(txtAfter.Text & vbNewLine)
  22.             End If
  23.             'FileClose(intFileNum)
  24.             objRead.Close()
  25.  
  26.             'Clear the text in the file and write into the file
  27.             objFile.WriteAllText(FILE_NAME, LineAll.ToString())
  28.         End If
  29.  
  30.         If (LineAll.Length > 0) Then
  31.             RichTextBox1.Text = LineAll.ToString()
  32.         End If
Dec 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.