可以使用Path类的GetFileName方法来获取路径中的文件名,示例代码如下:

string path = @"C:\Users\UserName\Documents\example.txt";
string fileName = Path.GetFileName(path);
Console.WriteLine(fileName); // 输出 "example.txt"

如果路径中包含目录,则只会返回文件名部分,而不包括目录部分。如果路径为空或者不包含文件名,则返回空字符串。

c# 从路径获取文件名

原文地址: https://www.cveoy.top/t/topic/fg5W 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录