我需要使用提取路径信息Path.GetFileName(),当输入字符串的最后一个字符是DirectorySeparatorChar('/'或'\')时,此函数不起作用.
我提出了这个代码,但它太长了.还有更好的方法吗?
string lastCharString = fullPath.Substring (fullPath.Length-1);
char lastChar = lastCharString[0];
if (lastChar == Path.DirectorySeparatorChar) {
fullPath = fullPath.Substring(0, fullPath.Length-1);
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*zun 54
fullPath = fullPath.TrimEnd(Path.DirectorySeparatorChar);
Run Code Online (Sandbox Code Playgroud)
Rye*_*eol 14
// If the fullPath is not a root directory
if (Path.GetDirectoryName(fullPath) != null)
fullPath = fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
Run Code Online (Sandbox Code Playgroud)