首页主机资讯c#获取文件名的方法有哪些

c#获取文件名的方法有哪些

时间2024-06-02 14:40:03发布访客分类主机资讯浏览1073
导读:在C#中,获取文件名的方法有以下几种方式: 使用Path类的GetFileName方法: string fileName = Path.GetFileName(filePath ; 使用FileInfo类的Name属性: File...

在C#中,获取文件名的方法有以下几种方式:

  1. 使用Path类的GetFileName方法:
string fileName = Path.GetFileName(filePath);
    
  1. 使用FileInfo类的Name属性:
FileInfo fileInfo = new FileInfo(filePath);
    
string fileName = fileInfo.Name;
    
  1. 使用LastIndexOf和Substring方法:
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
    
  1. 使用正则表达式:
Regex regex = new Regex(@"[^\\]*$");
    
Match match = regex.Match(filePath);
    
string fileName = match.Value;
    

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: c#获取文件名的方法有哪些
本文地址: https://pptw.com/jishu/673573.html
c#怎么获取文件夹所有文件 redis如何保证数据一致性

游客 回复需填写必要信息