【问题标题】:GetFiles exclude a stringGetFiles 排除字符串
【发布时间】:2018-07-18 10:55:26
【问题描述】:

使用以下代码,我可以获得名称中包含 string 的所有文件:

DirectoryInfo dirInf = new DirectoryInfo(path);
FileInfo[] fInfArray = dirInf.GetFiles("*string*");

但是如何排除GetFiles处的字符串
我不是说像.txt 这样的结尾我想在文件名中排除string

例如我有以下文件:

wordwordfile.msg  
wordwordfile.txt  
wodfile.txt

并排除 wod 得到以下文件:

wordwordfile.msg  
wordwordfile.txt  

【问题讨论】:

    标签: c# getfiles directoryinfo


    【解决方案1】:
    var files = new DirectoryInfo("C:\\")
                    .EnumerateFiles("*string*")
                    .Where(f => !f.Name.Contains("wod"))
                    // Optional, convert to array if you want
                    .ToArray();
    

    【讨论】:

    • GetFiles()改成EnumerateFiles(),去掉无意义的ToList()
    • 好声音 - 不知道这种方法。
    • 等待.. 列表仍然包含 "wod" :(
    • 你能发布你的实现吗?是案例问题吗?
    • 或许可以试试!f.Name.ToLower().Contains
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2013-04-19
    相关资源
    最近更新 更多