xxxxxxxxxx
using System;
using System.IO;
class Program
{
static void Main()
{
try
{
string path = "file.txt"; // Path to the file
// Read the contents of the file
string content = File.ReadAllText(path);
// Display the content
Console.WriteLine(content);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
}
xxxxxxxxxx
string text = File.ReadAllText(@"c:\file.txt", Encoding.UTF8);
xxxxxxxxxx
string[] lines = File.ReadAllLines(@"c:\file.txt", Encoding.UTF8);
xxxxxxxxxx
using System;
using System.IO;
namespace FileHandlinDemo
{
class Program
{
static void Main(string[] args)
{
string FilePath = @"D:\MyFile.txt";
string data;
FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
using (StreamReader streamReader = new StreamReader(fileStream))
{
data = streamReader.ReadToEnd();
}
Console.WriteLine(data);
Console.ReadLine();
}
}
}
xxxxxxxxxx
using(StreamReader file = new StreamReader(textFile)) {
int counter = 0;
string ln;
while ((ln = file.ReadLine()) != null) {
Console.WriteLine(ln);
counter++;
}
file.Close();
}