Writing to a file using System.IO

Here's how you can write data to a file from another file in C#.

Please let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!


You probably know how data can be read from a file. Here's how you can write data to a file from another file in C#:


using System;
using System.IO;
class AppendText 
{ 
public  static  void   Main(String[] args) 
{
//Append the text from InputFile.txt to this file.
TextWriter outfile= File.AppendText("c:/CopytoFile.txt");
//Append text from this file to CopytoFile.txt.
TextReader infile = File.OpenText("c:/InFile.txt");
String Str;
while((Str = infile.ReadLine())!=null)
outfile.WriteLine(Str);
infile.Close();
outfile.Close();
}
}


Source: DotNetExtreme.com
This was last published in March 2003

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close