首先聲明一個帶完整路徑的字符串
var path = @"D:\test.txt";
- 獲取文件名(包含擴展名)
Console.WriteLine(Path.GetFileName(path));
打印結果:
test.txt
- 獲取文件名(不包含擴展名)
Console.WriteLine(Path.GetFileNameWithoutExtension(path));
打印結果:
test
- 獲取擴展名
Console.WriteLine(Path.GetExtension(path));
打印結果:
txt