read a file and write it to another file in csharp

main.cs
// create the input and output file paths
string inputFile = @"C:\input.txt";
string outputFile = @"C:\output.txt";

// create a StreamReader to read the input file
using (StreamReader reader = new StreamReader(inputFile))
{
    // create a StreamWriter to write to the output file
    using (StreamWriter writer = new StreamWriter(outputFile))
    {
        // read the input file line by line
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            // write each line to the output file
            writer.WriteLine(line);
        }
    }
}
581 chars
20 lines

This code uses the StreamReader class to read the input file one line at a time. It then uses a StreamWriter class to write each line to the output file. The using statement is used to ensure that the file objects are properly closed and disposed.

gistlibby LogSnag