सबसे पहले, StreamWriter क्लास का उपयोग करके एक फ़ाइल बनाएं और उसमें सामग्री जोड़ें:
using (StreamWriter sw = new StreamWriter("hello.txt")) { sw.WriteLine("This is demo line 1"); sw.WriteLine("This is demo line 2"); sw.WriteLine("This is demo line 3"); }अब ReadAllLines() मेथड का उपयोग करके सभी पंक्तियों को पढ़ें। इसके साथ, पंक्तियों की संख्या प्राप्त करने के लिए Length प्रॉपर्टी का उपयोग करें:
int count = File.ReadAllLines("hello.txt").Length;यहाँ पूरा कोड है:
उदाहरण
using System; using System.Collections.Generic; using System.IO; public class Program { public static void Main() { using (StreamWriter sw = new StreamWriter("hello.txt")) { sw.WriteLine("This is demo line 1"); sw.WriteLine("This is demo line 2"); sw.WriteLine("This is demo line 3"); } int count = File.ReadAllLines("hello.txt").Length; Console.WriteLine("पंक्तियों की संख्या: "+count); } }आउटपुट
पंक्तियों की संख्या: 3