ในการจัดการเส้นทางของไฟล์ คลาส Path ถูกใช้ใน C#
ตั้งชื่อไฟล์เป็นสตริง -
string myPath = "D:\\new\\quiz.txt";
ตอนนี้ ในการรับส่วนขยาย ให้ใช้วิธี GetExtension() -
Path.GetExtension(myPath)
นี่คือรหัสที่สมบูรณ์ -
ตัวอย่าง
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