ASP.NET (C#, VB.NET)

ASP.NET C# วิธีอ่าน Text File

เมื่อ เขียนโปรแกรมบ่อยครั้งที่เราต้องการอ่านแฟ้มที่เก็บตัวอักษรล้วนๆ หรือ text file เพื่ออ่านข้อมูลบางอย่างที่มีขนาดเล็กและทำงานได้รวดเร็ว โค้ดต่อไปนี้อ่านไฟล์ชื่อ Data.txt ซึ่งอยู่ที่รูทไดเรคทอรีของฮาร์ดดิสก์ (โฟลเดอร์ c:) หัวใจของการทำงานคือการเรียกใช้ไทป์ StreamReader ของ .Net

public class FileClass
{
public static void Main()
{
ReadFromFile("c:\data.txt");
}
static void ReadFromFile(string filename)
{
StreamReader SR;
string S;
SR = File.OpenText(filename);
S = SR.ReadLine();
while (S != null)
{
Console.WriteLine(S);
S = SR.ReadLine();
}
SR.Close();
}
}

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Leave a Reply

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน