C# - Write text to a file
The code to write some text to a file.
CODE
using System;
using System.IO;
public class Program
{
public static void Main(string[] args)
{
//call the function
string fileFullName = @"C:\Test\Test\sample.txt";
//prepare the text to write to the file
// \r\n indicates a new line
string fileContents = "Apple";
fileContents += "\r\nBanana";
fileContents += "\r\nSome other fruit";
//call the function
WriteToFile(fileFullName, fileContents);
Console.Write("\nPress any key to continue: ");
Console.ReadKey();
}
public static void WriteToFile(string fileFullName, string fileContents)
{
//write the text to the file
//if the file already exists, it will be overwritten
File.WriteAllText(fileFullName, fileContents);
}
}
INPUT & OUTPUT
Comments
Post a Comment