What is the use of StreamWriter in C#?

 Posted by vishalneeraj-24503 on 1/28/2014 | Category: C# Interview questions | Views: 1600 | Points: 40
Answer:

StreamWriter is a class used in Dot Net to write Text into files.As name suggests it writes data to a byte stream.Actually it implements a System.IO.TextWriter class that writes characters to a byte stream in a particular encoding.

We have to give a proper file path in its constructor otherwise it will give FileNotFound error.

Syntax:
DirectoryInfo[] dirs = new DirectoryInfo(@"c:\").GetDirectories();


using (StreamWriter sw = new StreamWriter("name_of_the_file"))
{
foreach (DirectoryInfo dir in dirs)
{
sw.WriteLine(dir.Name);
}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response