开发者

c# Directory.GetFiles file structure from app root

开发者 https://www.devze.com 2023-04-10 19:48 出处:网络
I have the following piece of code: string root = Path.GetDirectoryName(Application.ExecutablePath); List<string> FullFileList = Directory.GetFiles(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

精彩评论

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

关注公众号