# Append text to text file first line

Hello EE,

Im trying to add strings to a text file, but i want to add at the beginning of the file, not at the end. Right now, im doing this :

If My.Computer.FileSystem.FileExists(mydocpath + "//text.txt") Then
Using sw As StreamWriter = File.AppendText(mydocpath + "\text.txt")
For Each kvp As KeyValuePair(Of Date, String) In MyItems
sw.WriteLine(kvp.Key + ";" + kvp.Value)
Next
End Using
End If

but it adds at the end... what could I do ?
LVL 1
Asked:
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

"Batchelor", Developer and EE Topic AdvisorCommented:
You can't "append" at the top of a file. You have to copy the file with the changes. That is, write the new lines in reversed order, than append the old content.
If you doing that frequently (e.g. every second or so), and files can get large - don't. All tools to watch progress in log files are optimized to look at the tail, because that is orders of magnitude faster.

There are implementations of log files writing new content to the top, but they often cut off the file at a certain size or number of lines, and "rotate" content out of the file (dismissing or archiving it). This can be done efficiently.
Commented:
So you want to "prepend" the data? If so, then you need to start a new file, add the text to be prepended, then add the text from the original file.
Commented:
HI,
pls try something like this
        If My.Computer.FileSystem.FileExists(mydocpath + "//text.txt") Then
using (StreamReader sreader = new StreamReader(mydocpath + "//text.txt") ))
{
str = sreader.ReadToEnd();
}

File.Delete(mydocpath + "//text.txt");

using (StreamWriter swriter = new StreamWriter(mydocpath + "//text.txt", false))
{
For Each kvp As KeyValuePair(Of Date, String) In MyItems
swriter .WriteLine(kvp.Key + ";" + kvp.Value)
Next
swriter.Write(str);
}
End If

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
thanks
Author Commented:
Hey Kaufmed, or anybody else.. a nice question for you here if you can help me.

http://www.experts-exchange.com/questions/28697160/Speed-up-my-loops.html

thanks
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.