5 ways to write to a file with C# .NET
December 16, 2016 Leave a comment
It’s a common scenario that you need to write some content to a file. Here you see 5 ways to achieve that.
1. Using a FileStream:
private void WriteToAFileWithFileStream()
{
using (FileStream target = File.Create(@"c:\mydirectory\target.txt"))
{
using (StreamWriter writer = new StreamWriter(target))
{
writer.WriteLine("Hello world");
}
}
}