ใช้ FileInfo.Length Property ดึงขนาดไฟล์เป็นไบต์ (long)
ตัวอย่างโค้ด
using System;
using System.IO;
class Program
{
static void Main()
{
FileInfo file = new FileInfo(@"D:\new");
long res = file.Length;
Console.WriteLine("Bytes: " + res);
}
}
ผลลัพธ์ (ตัวอย่าง)
Bytes: 3259244
หมายเหตุ: FileInfo ต้องการพาธไฟล์ที่มีอยู่จริง มิฉะนั้น Length จะคืน 0 หรือโยน Exception ขึ้นอยู่กับเวอร์ชัน .NET