在字符串中设置路径名-
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