Linux 拨号vps windows公众号手机端

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

lewis 1年前 (2024-03-17) 阅读数 14 #程序编程
文章标签 c#

在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;
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门