Exclude certain file extensions when getting files from a directory

后端 未结 10 1996
一个人的身影
一个人的身影 2020-12-02 22:00

How to exclude certain file type when getting files from a directory?

I tried

var files = Directory.GetFiles(jobDir);
10条回答
  •  庸人自扰
    2020-12-02 22:40

    I know, this a old request, but about me it's always important.

    if you want exlude a list of file extension: (based on https://stackoverflow.com/a/19961761/1970301)

    var exts = new[] { ".mp3", ".jpg" };
    
    
    
    public IEnumerable FilterFiles(string path, params string[] exts) {
        return
            Directory
            .GetFiles(path)
            .Where(file => !exts.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase)));
    }
    

提交回复
热议问题