0

I have a form that is similar to a diary. I would like the user to make notes and the form would save the notes in the textbox to a text file. The data appends to the text file, but I want it to append to the top so when it loads, it is the first line of text the user is able to see.

IO.File.AppendAllText("name.txt", vbCrLf + Date.Today + vbCrLf + TextBox9.Text)

Is there any way to have the text be inserted to the top of the text file?

ry b
  • 87
  • 2
  • 10
  • See http://stackoverflow.com/questions/1343044/c-prepending-to-beginning-of-a-file – Mihai8 Mar 12 '13 at 21:23
  • In addition to reading the suggestions in the above thread posted by user1929959, I would consider saving each entry in its own file. It would be much easier, for instance, to grab the 10 most recent entries and each file would then have its own timestamp. It would also reduce the chance of having extremely slow IO operations if that file grew large. Also, is there any reason using a database is out of the question? – Jacob VanScoy Mar 12 '13 at 21:40
  • I would be very cautious about implementing suggestions from people that do not have all the design parameters. For example, @JacobVanScoy's suggestion about the multiple files will become a pitfall if the number of files grows past ~1000. – Sam Axe Mar 12 '13 at 21:48
  • When you say 'text file' do you mean you want to treat the file as one contiguous block of text? B/c if not, you could have multiple blocks of text as a linked list or similar within the same file, in any order you want. Then you can append to the file, but display the blocks of text in your preferred order. Sort of half way to a database. – peterG Mar 12 '13 at 22:14
  • 1
    You could save the data as XML (appending to the file instead of prepending), which could help if TextBox9.Text included VbCrLf sequences (encode them as ` `), then display the entries in reverse date order. Or if VbCrLf is not a problem, just display the entries in reverse order. – Andrew Morton Mar 12 '13 at 22:38
  • it's much easier to 'append' to file - and then 'stack up' and push to 'screen'/textbox in any order you want. – NSGaga-mostly-inactive Mar 13 '13 at 01:27

3 Answers3

2

Get the content of your name.txt and put it in a variable, then append your new text before it.
Something like this:

Dim mytext as String 
mytext = Read(name.txt)  
mytext = Date.today & vbCrlf & Textbox9.text & vbCrlf & name.txt  
IO.File.WriteAllText("name.txt", mytext)  

The downside of this method is that the write time will increase whenever your text increases.

Ruben_PH
  • 1,692
  • 7
  • 25
  • 42
  • What if file is very large in size say 20mb. in this case reading all text will be time consuming. any other possible way? – Pramod Apr 30 '15 at 10:44
0

Read the existing log, prepend new entry, write log.

Won't scale all that well but should work OK for a while. Perhaps switch logs by Day/month/year depending on volume of new entries.

rheitzman
  • 2,247
  • 3
  • 20
  • 36
0

Put the file into a variable, then simply add the old text to the end. Something like this:

Dim TextFileReader as String
TextFileReader = My.Computer.FileSystem.ReadAllText("path")
IO.File.WriteAllText("path", "Our text to go at the top" & vbnewline & TextFileReader)
Josh Face
  • 107
  • 12