开发者

c# Directory.GetFiles file structure from app root

I have the following piece of code:

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

Now this works very well, however the file names with it are quire long. is t开发者_如何学Pythonhere a way i can take out the path till root? but still show all the subfolders?

Root = C:\Users\\Desktop\Test\

But the code would return the whole path from C: while I'd prefer if I could take out the root bit straight away. but still keep the file structure after it.

eg C:\Users\\Desktop\Test\hi\hello\files.txt would return \hi\hello\files.txt

I know i can just iterate over the file list generated and remove it all one by one, I'm wondering if I can just filter it out stright.


Using the power of 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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜