c#目录.从应用根目录获取文件结构

本文关键字:获取 文件结构 根目录 应用 目录 | 更新日期: 2023-09-27 18:10:37

我有以下一段代码:

string root = Path.GetDirectoryName(Application.ExecutablePath);
List<string> FullFileList = Directory.GetFiles(root, "*.*",
     SearchOption.AllDirectories).Where(name =>
          { 
              return !(name.EndsWith("dmp") || name.EndsWith("jpg"));
          }).ToList();

现在这个工作得很好,但是文件名很长。有没有办法把路径移到根?但仍然显示所有子文件夹?

Root = C:'Users''Desktop'Test'

但是代码会从C返回整个路径:而我更希望我能直接把根部拔出来。但仍然保留它之后的文件结构。

如C:'Users'桌面' '测试' ' hello ' files.txt你好将返回'你好' hello ' files.txt

我知道我可以迭代生成的文件列表并逐个删除它,我想知道我是否可以直接过滤它

c#目录.从应用根目录获取文件结构

使用LINQ的强大功能:

string root = Path.GetDirectoryName(Application.ExecutablePath);
List<string> FullFileList = Directory.GetFiles(root, "*.*", SearchOption.AllDirectories)
    .Where(name =>
    { 
        return !(name.EndsWith("dmp") || name.EndsWith("jpg"));
    })
    .Select(file => file.Replace(root, "")
    .ToList();