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

Количество файлов из папки

Как получить количество файлов из папки с помощью ASP.NET с помощью С#?

4b9b3361

Ответ 1

System.IO.Directory myDir = GetMyDirectoryForTheExample();
int count = myDir.GetFiles().Length;

Ответ 2

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

Также см. Directory.GetFiles-метод (String, String, SearchOption)

Вы можете указать опцию поиска в этой перегрузке.

TopDirectoryOnly: включает только текущий каталог в поиске.

AllDirectories. Включает текущий каталог и все подкаталоги в операции поиска. Этот параметр включает точки повторной обработки, такие как установленные диски и символические ссылки в поиске.

// searches the current directory and sub directory
int fCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length;
// searches the current directory
int fCount = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).Length;

Ответ 3

Самый слабый метод - использовать LINQ:

var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
                        select file).Count();

Ответ 4

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("SourcePath");
int count = dir.GetFiles().Length;

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

Ответ 5

Чтение PDF файлов из каталога:

var list = Directory.GetFiles(@"C:\ScanPDF", "*.pdf");
if (list.Length > 0)
{

}

Ответ 6

Попробуйте выполнить следующий код, чтобы получить количество файлов в папке

 string strDocPath = Server.MapPath('Enter your path here'); 
    int docCount = Directory.GetFiles(strDocPath, "*", 
    SearchOption.TopDirectoryOnly).Length;

Ответ 7

Чтобы получить количество определенных расширений типов с помощью LINQ, вы можете использовать этот простой код:

Dim exts() As String = {".docx", ".ppt", ".pdf"}

Dim query = (From f As FileInfo In directory.GetFiles()).Where(Function(f) exts.Contains(f.Extension.ToLower()))

Response.Write(query.Count())