एक सादा पाठ फ़ाइल खोलने के लिए, 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".