471,338 Members | 1,196 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

How do I append a text file to another text file

Hi,

How could I append a text file to another text file. Let's say that I have a
File1 and I want to append File2 at the end of File1, how would I do that?

TIA
Roy
Dec 8 '05 #1
2 8349
Have a look at the following code from MSDN library.

Cheers,

Denis

using System;
using System.IO;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}

// This text is always added, making the file longer over time
// if it is not deleted.
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}

// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
}

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:Kn*******************@wagner.videotron.net...
Hi,

How could I append a text file to another text file. Let's say that I have a File1 and I want to append File2 at the end of File1, how would I do that?

TIA
Roy

Dec 8 '05 #2

Thanks Denis it worked like a charm.

Roy
*** Sent via Developersdex http://www.developersdex.com ***
Dec 8 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JMCN | last post: by
14 posts views Thread by vbMark | last post: by
3 posts views Thread by Macca | last post: by
2 posts views Thread by Gaby Sandoval | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.