ตัวอย่างโค้ดภาษา C# ที่ใช้สำหรับการอ่าน Text ไฟล์
วิธีการใช้
File.ReadAllText(“ที่อยู่ของไฟล์ที่จะอ่าน”);
ตัวอย่างโค้ด :
using System;
// อย่าลืม using System.IO นะครับ
using System.IO;
namespace ComSciDev.Com.CSharp.ReadTextFile
{
class Program
{
static void Main(string[] args)
{
try
{
// File.ReadAllText จะคืนค่าเป็น string
string text = File.ReadAllText(@"C:\textfile.txt");
Console.Write(text);
}
// แสดง Error หากไม่สามารถอ่านไฟล์ได้
catch(Exception ex) {
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
