Đặt tên đường dẫn trong một chuỗi -
string myPath = "D:\\new\\quiz.txt";
Bây giờ, sử dụng phương thức GetFileName () để lấy tên của tệp -
Path.GetFileName(myPath)
Sau đây là mã hoàn chỉnh -
Ví dụ
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));
}
}
} Đầu ra
Extension: .txt File Path: D:\new\quiz.txt