using System.IO; FileInfo file = new FileInfo(@"C:\temp\myfile.txt"); using StreamWriter writer = file.CreateText(); writer.WriteLine("Hello, World!"); writer.Close();
using System.IO; string fileName = "myTextFile.txt"; string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName); using StreamWriter writer = new FileInfo(path).CreateText(); writer.WriteLine("This is my text file."); writer.Close();In this example, we create a new file "myTextFile.txt" on the desktop of the local computer using a combination of the Path class and the Environment class. We then write a line "This is my text file." to the file using the StreamWriter object returned by the CreateText() method. Package Library: System.IO.FileSystem.