943,778 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3245
  • C# RSS
Dec 17th, 2008
0

Appending the content of a text file to another text file

Expand Post »
Hello there, I've been busy nowadays with file manipulation and I've so far I've encountered a small problem in appending the contents of a file to another file. This is what I did:

C# Syntax (Toggle Plain Text)
  1.  
  2. string filename = @"C:\Temp.txt";
  3. string body = null;
  4. StreamReader sr = File.OpenText(files);
  5. body = sr.ReadToEnd();
  6. StreamWriter sw = File.Create(filename);
  7. sw.WriteLine(body);
  8.  
  9. sr = File.OpenText(files2);
  10. body = sr.ReadToEnd();
  11. File.AppendAllText(filename, body);
  12.  
  13. sw.Close();

It seems that I can't append the content of files2 to my Temp.txt file after placing the contents of files. Please help. Thanks.
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster
murderotica is offline Offline
171 posts
since Aug 2008
Dec 17th, 2008
0

Re: Appending the content of a text file to another text file

Why not just do the writeline of body you read from the second file?
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Dec 17th, 2008
0

Re: Appending the content of a text file to another text file

Thanks for the reply Ma'am. Will it still append?
Reputation Points: 7
Solved Threads: 2
Junior Poster
murderotica is offline Offline
171 posts
since Aug 2008
Dec 17th, 2008
0

Re: Appending the content of a text file to another text file

Hello again, it seems it is working. I will try it with more than two text files. Thank you.
Reputation Points: 7
Solved Threads: 2
Junior Poster
murderotica is offline Offline
171 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: File details in to List View using a file dialog box
Next Thread in C# Forum Timeline: Memory Leak





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC