我有下面定义的两个列表
IEnumerable<string> fileNames = new DirectoryInfo(Path)
.EnumerateFiles("fileServer.config", SearchOption.AllDirectories)
.Select(fi => fi.DirectoryName)
.Select(dirPath => dirPath.Substring(Path.Length));
List<string> subFolderNames = fileNames.ToList();
但是此subFolderNames的输出在每个元素的前面都带有\\,像这样
\\ ElementName
如何摆脱这个 \\ ?
答案 0 :(得分:3)
从技术上讲,您可以Trim
个不需要的字符(它们是目录分隔符):
...
.Select(dirPath => dirPath
.Substring(Path.Length)
.TrimStart(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar));
或更改Substring
参数:
...
.Select(dirPath => dirPath.Substring(Path.Length + 1));
但是,如果您想要最大的目录名称
IEnumerable<string> fileNames = new DirectoryInfo(Path)
.EnumerateFiles("fileServer.config", SearchOption.AllDirectories)
.Select(fi => fi.Name);