如何摆脱 IEnumerable 列表中的正斜杠“\\”
How can I Get rid of the Forward slashes " \\ " in an IEnumerable List
我有下面定义的两个列表
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 的输出在每个元素的前面都有一个 \\,就像这样
\\元素名称
我怎样才能摆脱这个\\?
从技术上讲,您可以 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);
我有下面定义的两个列表
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 的输出在每个元素的前面都有一个 \\,就像这样
\\元素名称
我怎样才能摆脱这个\\?
从技术上讲,您可以 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);