-
利用字符串分割法
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); //获取文件名
其中,第二种方法更加思路更加简单,较为常用。
本文介绍了两种从完整文件路径中分离出文件路径和文件名的方法:一是使用字符串分割法,二是利用System.IO.Path类提供的便捷方法。通过具体示例展示了如何在C#中高效地进行文件路径和文件名的提取。
376

被折叠的 条评论
为什么被折叠?



