append to the beginning of a text file

  • Thread starter Thread starter Eddie Suey
  • Start date Start date
E

Eddie Suey

I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.
 
* "Eddie Suey said:
I want to add a new line to the begining of a text file. I dont want to
write over existing data. How do I do this? the file is about 7 mb.

You will have to move the whole contents of the file...
 
trick:

(1) copy the existing data of notepad file to a textbox1
(2) delete the file.
(3) create the file with same name and copy the new text (which you want to
put in the beginning)
(4) now append the file and copy the contents of textbox1
 
Hi Eddie,
That is one of first reasons why people started to make databases.
You can now even do more with it,
Maybe a good idea to look if a simple database is not a better approach for
you.
Cor
 
* "Jay B. Harlow said:
Largely because of the size of the file. I would consider writing the new
line to a new file, then using Asynchronous File I/O to append the existing
file on the end of this new file, delete the old file, then rename the new
file to the original name...

This will change the creation timestamp of the file.
 
Herfried,
If the creation timestamp is important, then this would be a downside of the
method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read
write, you could 'copy' the attributes from the original file's FileInfo to
the new file's FileInfo and that would update the actual file, thus
preserving the creation timestamp...

Hmm... Touch.NET (simple sample Console Application demonstrating the above)

Public Module MainModule

' args(0) = file to change
' args(1) = properly formatted creation date time

Public Sub Main(ByVal args() As String)
Dim fi As New IO.FileInfo(args(0))
fi.CreationTime = DateTime.Parse(args(1))
End Sub

End Module

Which will set the creation time of the file given to the date given.

Something like:
Touch myfile.txt 10/1/2003

Hope this helps
Jay
 
* "Jay B. Harlow said:
If the creation timestamp is important, then this would be a downside of the
method.

However! ;-)

I have not played with IO.FileInfo enough, the CreationTime property is read
write, you could 'copy' the attributes from the original file's FileInfo to
the new file's FileInfo and that would update the actual file, thus

This will work. It was only a little note because most people forget
that the creation date may sometimes be useful...
 
Herfried,
Hey! its an important caveat to note!

Not only that it gave me an opportunity to play with System.IO.FileInfo,
something I have not played with a lot.

Thanks
Jay
 
Back
Top