在C#中获取文件名的方法通常使用System.IO命名空间中的Path类。具体来说,可以使用Path类中的GetFileName方法来获取文件的文件名。示例如下:
usingSystem;
usingSystem.IO;
classProgram
{
staticvoidMain()
{
stringfilePath=@"C:\Users\user\Desktop\example.txt";
stringfileName=Path.GetFileName(filePath);
Console.WriteLine("FileName:"+fileName);
}
}
上面的示例代码中,首先定义了一个文件路径filePath,然后使用Path.GetFileName方法获取文件名,最后输出文件名到控制台。