Подтвердить что ты не робот

Исключить расширение файла в System.IO.Directory.GetFiles()

Есть ли способ получить количество файлов в папке, но я хочу исключить файлы с расширением jpg?

Directory.GetFiles("c:\\Temp\\").Count();
4b9b3361

Ответ 1

Попробуйте следующее:

var count = System.IO.Directory.GetFiles(@"c:\\Temp\\")
                               .Count(p => Path.GetExtension(p) != ".jpg");

Удачи!

Ответ 2

Вы можете использовать объект DirectoryInfo в каталоге и сделать GetFiles() на нем с фильтром.

Ответ 3

Использование метода Linq Where:

Directory.GetFiles(path).Where(file => !file.EndsWith(".jpg")).Count();

Ответ 4

public static string[] MultipleFileFilter(ref string dir)
{
    //determine our valid file extensions
    string validExtensions = "*.jpg,*.jpeg,*.gif,*.png";

    //create a string array of our filters by plitting the
    //string of valid filters on the delimiter
    string[] extFilter = validExtensions.Split(new char[] { ',' });

    //ArrayList to hold the files with the certain extensions
    ArrayList files = new ArrayList();

    //DirectoryInfo instance to be used to get the files
    DirectoryInfo dirInfo = new DirectoryInfo(dir);

    //loop through each extension in the filter
    foreach (string extension in extFilter)
    {
        //add all the files that match our valid extensions
        //by using AddRange of the ArrayList
        files.AddRange(dirInfo.GetFiles(extension));
    }

    //convert the ArrayList to a string array
    //of file names
    return (string[])files.ToArray(typeof(string));
}

Должен работать

Алекс

Ответ 5

Вы можете просто использовать простой оператор LINQ для отсечения JPG.

Directory.GetFiles("C:\\temp\\").Where(f => !f.ToLower().EndsWith(".jpg")).Count();

Ответ 6

string[] extensions = new string[] { ".jpg", ".gif" };

var files = from file in Directory.GetFiles(@"C:\TEMP\")
            where extensions.Contains((new FileInfo(file)).Extension)
            select file;

files.Count();

Ответ 7

Вы можете использовать предложение LINQ 'Where' для фильтрации файлов с не требуемым расширением.

Ответ 8

System.IO.Directory.GetFiles("c:\\Temp\\").Where(f => !f.EndsWith(".jpg")).Count();

Ответ 9

Вы всегда можете использовать LINQ.

return GetFiles("c:\\Temp\\").Where(str => !str.EndsWith(".exe")).Count();