C# - Read file contents all at once
The code to read the contents of a file all at once and display its contents.
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";
ReadFile(fileFullName);
Console.Write("\nPress any key to continue: ");
Console.ReadKey();
}
public static void ReadFile(string fileFullName)
{
//check if the file exists
if (File.Exists(fileFullName))
{
//if the file exists, proceed with the logic
//read all the contents of the file
string fileContents = File.ReadAllText(fileFullName);
//display the contents of the file
Console.WriteLine(fileContents);
}
else
{
//if the file doesn't exists, display an error message.
Console.WriteLine("The File {0} does not exist.", fileFullName);
}
}
}
INPUT & OUTPUT
Comments
Post a Comment