C#分割文件路径中的文件名与路径

  • 利用字符串分割法

int lastIndex = filepath.LastIndexOf("\\");  //  \\的意思是,一个是转义,一个是代表斜杠
string pFilePath = filepath.Substring(0, lastIndex);  //文件路径
string pFileName = filepath.Substring(lastIndex + 1);  //文件名
  • 利用System.IO.Path提供的方法

string pPath = System.IO.Path.GetDirectoryName(pFullpath); //获取文件路径
string pName = System.IO.Path.GetFileName(pFullpath); //获取文件名

其中,第二种方法更加思路更加简单,较为常用。

猜你喜欢

转载自blog.csdn.net/ScapeD/article/details/82809807