প্রথমত, 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() পদ্ধতি ব্যবহার করুন। এর সাথে, দৈর্ঘ্যের বৈশিষ্ট্যটি লাইনের সংখ্যা −
পেতে ব্যবহার করতে হবে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("Number of lines: "+count);
}
} আউটপুট
Number of lines: 3