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