-1

Possible Duplicate:
C#: Prepending to beginning of a file
Adding a Line to the Middle of a File with .NET

I have a file.txt with text inside and I need to know if is possible to writeother text in the begining of this file ?

I mean: There's a text and I want to write something else without loosing what I already have inside the file.txt. BUT, I want to insert this new text in the begining of the file... Is it possible?

I'm using StreamWriter to write inside the file. Just need to know if it's possible and the method to do so.

Community
  • 1
  • 1
Ghaleon
  • 1,186
  • 5
  • 28
  • 55
  • Without solutions to my problem. Thanks anyway – Ghaleon Feb 05 '13 at 13:21
  • You should read this article :- http://stackoverflow.com/questions/1343044/c-prepending-to-beginning-of-a-file – Derek Feb 05 '13 at 13:24
  • Its too easy mark as a duplicated without saying where is the duplicate ,isn't it ? The both links two of you commented here(and now you've erased it) were way different than my problem, you two just needed to read first ! – Ghaleon Feb 05 '13 at 13:26
  • @Derek thanks, I already read that... Someone marked as duplicate then commmented the same link you just did, then erased the comment, its not duplicated.... Thanks anyway – Ghaleon Feb 05 '13 at 13:27
  • -1 for posting you don't understand basic code Ghaleon – MethodMan Feb 05 '13 at 13:27
  • Read again what I commented about your code =) you should get -1 for posting code that has nothing to do with the question – Ghaleon Feb 05 '13 at 13:29
  • @Ghaleon I am confused.... the first link _exactly_ answers your question, it even provides you the code needed to accomplish this. Either you are not correctly expressing the problem or you do not understand the code presented? – PhoenixReborn Feb 05 '13 at 14:17

2 Answers2

2

One way is to place all the text in a string :

using System.IO;

StreamReader streamReader = new StreamReader(filePath);
string initialText = streamReader.ReadToEnd();
streamReader.Close(); 

Then add to the beginning of the string:

endText = textAtBeginning & initialText;

Then finally write endText to replace all text within your file:

// Write the string to a file, overwriting the existing text. 

StreamWriter myWriter = new StreamWriter("c:\\test.txt", false))
myWriter.WriteLine(endText);
myWriter.Close();
Kurren
  • 827
  • 1
  • 9
  • 18
  • Got it ! I'll try. I just read the existing file and hold it's text into a `string` then I do the same to the new text then just write it all ? – Ghaleon Feb 05 '13 at 13:24
  • Yep. If "New text" is a text file, read it and put it in a string. Then place at the beginning of your string and overwrite the file. – Kurren Feb 05 '13 at 13:28
  • Edit: I added the code to overwrite the file – Kurren Feb 05 '13 at 13:33
  • I tried here, then I read the file to the string, its values is all in a single line... – Ghaleon Feb 05 '13 at 16:02
0

Try this this is best way to do it

File.WriteAllText("debug.txt", "My name is Test");

        using (Stream stream = new FileStream("debug.txt", FileMode.OpenOrCreate))
        {
            stream.Seek(0, SeekOrigin.Begin);
            stream.Write(ASCIIEncoding.ASCII.GetBytes("whatever you text is"), 0, ASCIIEncoding.ASCII.GetBytes("whatever you text is").Length);
        }

You must use seek property for writing as the previous example of reading all content and deleting the file and creating new file is not a good practice it's a problem when you have large files so use this is the the best way to do it. Seek to the position where you want to write and then write it.

sumeet kumar
  • 2,628
  • 1
  • 16
  • 24
  • Could you explain it to me please? I dont get it ;s sorry – Ghaleon Feb 05 '13 at 13:22
  • Ghaleon, sorry to sound so rude but how can you be a programmer if you can't understand basic coding? do you know how to use the `Debugger?` if so step thru the example ..if not then use `google` to hit `MSDN` and searh `Directory.GetCurrentDirectory()` `File.Delete` and the `StreamWriter Class` – MethodMan Feb 05 '13 at 13:26
  • @DJKRAZE it was not the code i commented... And your code I already know and its not what I asked,read again... Thanks anyway, and sorry if i've been rude... – Ghaleon Feb 05 '13 at 13:28