• 0

[C#] How do i insert a string to top of Text File?


Question

3 answers to this question

Recommended Posts

  • 0

I'm not sure of the most efficient way, but this way works:

string filename = "c:\\test.txt";

string entireFile = "";

if (File.Exists(filename))

{

StreamReader reader = new StreamReader(filename);

entireFile = reader.ReadToEnd();

reader.Close();

}

StreamWriter writer = new StreamWriter(filename);

writer.WriteLine("This line is pre-pended");

writer.Write(entireFile);

writer.Close();

  • 0
  benjeeeboy said:
Instead of opening it using FileMode.Append, use FileMode.Open and I think it'll move stuff back but I'm not totally sure

586737985[/snapback]

no, it wont work

AFAIK The only way you can do it is to open the file, copy everything in there, add your text, then paste the stuff from before.

so it would be somethin like what kjordan posted

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.