Exclude certain file extensions when getting files from a directory

后端 未结 10 1927
一个人的身影
一个人的身影 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:52

    You can try this,

    var directoryInfo = new DirectoryInfo("C:\YourPath");
    var filesInfo = directoryInfo.GetFiles().Where(x => x.Extension != ".pdb");
    
    0 讨论(0)
  • 2020-12-02 22:57

    I guess you can use lambda expression

    var files = Array.FindAll(Directory.GetFiles(jobDir), x => !x.EndWith(".myext"))
    
    0 讨论(0)
  • 2020-12-02 22:57

    I came across this looking for a method to do this where the exclusion could use the search pattern rules and not just EndWith type logic.

    e.g. Search pattern wildcard specifier matches:

    • * (asterisk) Zero or more characters in that position.
    • ? (question mark) Zero or one character in that position.

    This could be used for the above as follows.

    string dir = @"C:\Temp";
    var items = Directory.GetFiles(dir, "*.*").Except(Directory.GetFiles(dir, "*.xml"));
    

    Or to exclude items that would otherwise be included.

    string dir = @"C:\Temp";
    var items = Directory.GetFiles(dir, "*.txt").Except(Directory.GetFiles(dir, "*HOLD*.txt"));
    
    0 讨论(0)
  • 2020-12-02 23:00

    i used that

    Directory.GetFiles(PATH, "*.dll"))

    and the PATH is:

    public static string _PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

    0 讨论(0)
提交回复
热议问题