Here for an example on how to use
StreamWriter
and write text to log file:
StreamWriter Class (System.IO) | Microsoft Docs[
^]
namespace StreamReadWrite
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();
using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
{
foreach (DirectoryInfo dir in cDirs)
{
sw.WriteLine(dir.Name);
}
}
string line = "";
using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}
Another sample here:
Exception Logging to Text file[
^]