Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 21st, 2006, 02:38 PM
Registered User
Join Date: Aug 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Most efficient way to insert text into a file

I'm writing an app that creates a status text file. Any time there was a change I'd write to the file by:

Status_Message_Text = DateString + "   " + TimeString + "     " + Status_Message + vbCrLf
My.Computer.FileSystem.WriteAllText(File_Path, Status_Message_Text, True)
Elsewhere I display it in a Text Box:

txtStatusLog.Text = My.Computer.FileSystem.ReadAllText(File_Path)
It works fabulous except that the write appends the text at the end.

I'd like either a way to insert the text at the beginning of the file or somehow sort the display in the Text Box because I want to display the most recent status message first.

For now I changed the write so that the Status_Message_Text reads in the file and the write over-writes the file:

Status_Message_Text = DateString + "   " + TimeString + "     " + Status_Message + vbCrLf + My.Computer.FileSystem.ReadAllText(File_Path)
My.Computer.FileSystem.WriteAllText(File_Path, Status_Message_Text, False)
There's got to be a better way!

Any ideas?


Reply With Quote
  #2 (permalink)  
Old February 22nd, 2007, 01:09 PM
Registered User
Join Date: Feb 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

I just put an article on CodeProject that describes how to do this in C#:

Insert Text into Existing Files in C#, Without Temp Files or Memory Buffers

This uses a FileStream, a StreamReader, and a BinaryWriter to replace, delete, overwrite, or insert text at any desired position in the file. I know your main question was how to insert text into a file, but the other stuff might come in handy too.

The jist of it is that you could (a) buffer all the file contents that come after the insertion position, then write your text, and finally fetch the buffered text & write it back to the file, or (b) you can do this without any buffering or temp files by comparing the length of the text you're inserting with the length of the text that will come after it in the target file. Knowing that, you can shift the subsequent text just once, making exactly enough room for the text you want to insert.

Let me know if the article is clear and understandable; I'm happy to go back and try to improve the ugly stuff.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
insert a new line at beginning of text file? how? Satorikin VB.NET 2002/2003 Basics 8 October 15th, 2008 09:55 PM
how to insert text file in sql server database sharvari_mothe Java Databases 3 June 5th, 2007 07:25 AM
insert text into a text box by selecting an link gavmc Other Programming Languages 0 February 15th, 2006 09:46 AM
how can I insert a new line at top of text file? Satorikin VB.NET 1 March 4th, 2004 03:41 AM
Insert data in a text file and export them cutovoi Excel VBA 3 January 23rd, 2004 07:31 AM

All times are GMT -4. The time now is 06:39 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.