একটি প্লেইন টেক্সট ফাইল খোলতে StreamReader ক্লাস ব্যবহার করুন। নিচের কোডটি একটি ফাইল পড়ার জন্য খোলে:
StreamReader sr = new StreamReader("d:/new.txt")
এখন ফাইলের কন্টেন্ট প্রদর্শন করুন:
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
সম্পূর্ণ উদাহরণ
using System;
using System.IO;
namespace FileApplication
{
class Program
{
static void Main(string[] args)
{
try
{
using (StreamReader sr = new StreamReader("d:/new.txt"))
{
string line;
// ফাইল থেকে লাইন পড়ুন এবং প্রদর্শন করুন
// ফাইল শেষ পর্যন্ত
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// ব্যবহারকারীকে জানান কী ভুল হয়েছে
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
আউটপুট
The file could not be read: Could not find a part of the path "d:/new.txt".