获取文件夹文件目录忽略所选文件夹的顶部层次结构

本文关键字:文件夹 顶部 层次结构 文件目录 获取 | 更新日期: 2023-09-27 18:06:09

使用这行代码获取文件夹目录

string[] fileEntries = Directory.GetFiles(@"C:'Users'Desktop'WebApi'", "*",SearchOption.TopDirectoryOnly);

返回

C:'Users'Desktop'WebApi'COPYING.evaluation
C:'Users'Desktop'WebApi'Global.asax
C:'Users'Desktop'WebApi'MobileUserAgent.deploy-readme.txt
C:'Users'Desktop'WebApi'MobileUserAgent.deploy.cmd
C:'Users'Desktop'WebApi'MobileUserAgent.SetParameters.xml
C:'Users'Desktop'WebApi'MobileUserAgent.SourceManifest.xml
C:'Users'Desktop'WebApi'MobileUserAgent.zip
C:'Users'Desktop'WebApi'packages.config
C:'Users'Desktop'WebApi'Web.config

所需

'WebApi'COPYING.evaluation
'WebApi'Global.asax
'WebApi'MobileUserAgent.deploy-readme.txt
'WebApi'MobileUserAgent.deploy.cmd
'WebApi'MobileUserAgent.SetParameters.xml
'WebApi'MobileUserAgent.SourceManifest.xml
'WebApi'MobileUserAgent.zip
'WebApi'packages.config
'WebApi'Web.config

对的任何建议

获取文件夹文件目录忽略所选文件夹的顶部层次结构

foreach (var s in fileEntries)
{
    Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(s)) + "''" + Path.GetFileName(s));
}

List<string> lst = fileEntries.Select(s => Path.GetFileName(Path.GetDirectoryName(s)) + "''" + Path.GetFileName(s)).ToList<string>();
foreach (var item in lst)
{
    Console.WriteLine(item);
}

您可以简单地Substring结果:

string[] fileEntries = Directory.GetFiles(@"C:'Users'Desktop'WebApi'", "*", 
                                            SearchOption.TopDirectoryOnly)
                                .Select(x=> x.Substring( x.IndexOf("''WebApi"))).ToArray();