একটি প্লেইন টেক্সট ফাইল খুলতে, 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;
// Read and display lines from the file until
// the end of the file is reached.
while ((line = sr.ReadLine()) != null) {
Console.WriteLine(line);
}
}
} catch (Exception e) {
// Let the user know what went wrong.
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 "/home/cg/root/4281363/d:/new.txt".