ใช้ Path.GetExtension(path) เพื่อดึงนามสกุลไฟล์ (รวมจุดนำหน้า เช่น ".txt")
ตัวอย่างโค้ด
using System;
using System.IO;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string myPath = @"D:\new\quiz.txt";
Console.WriteLine(Path.GetExtension(myPath));
}
}
}
ผลลัพธ์
.txt
คืนค่า: สตริงที่เริ่มด้วย '.' หากไม่มีนามสกุล จะคืนสตริงว่าง ""