Using WriteAllText method
using System.IO;
using System;
class WriteFile
{
/* WriteAllText creates a file, writes the specified string to the file,
and then closes the file.*/
static void Main()
{
string fileLoc = "D://MyFile.txt";
FileStream fs = new FileStream(fileLoc, FileMode.Create);
fs.Close();
Console.WriteLine("File created successfully");
string text = "Hello World..!!";
File.WriteAllText(fileLoc, text); //Write one string to a text file.
Console.WriteLine("Written to file successfully");
Console.ReadLine();
}
}
Output:
File created successfully
Written to file successfully