Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2006, 02:38 PM
Registered User
 
Join Date: Aug 2006
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:

Code:
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:

Code:
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:

Code:
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?

THANKS!

 
Old February 22nd, 2007, 01:09 PM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






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





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