ตั้งชื่อเส้นทางเป็นสตริง -
string myPath = "D:\\new\\quiz.txt";
ตอนนี้ ใช้เมธอด GetFileName() เพื่อรับชื่อไฟล์ -
Path.GetFileName(myPath)
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.IO;
namespace Demo {
class Program {
static void Main(string[] args) {
string myPath = "D:\\new\\quiz.txt";
// get extension
Console.WriteLine("Extension: "+Path.GetExtension(myPath));
// get path
Console.WriteLine("File Path: "+Path.GetFileName(myPath));
}
}
} ผลลัพธ์
Extension: .txt File Path: D:\new\quiz.txt