First of all: Your method for counting pages is not reliable.
e.g. it gave me 0 for a PDF file actually containing over 1000 pages.
If you once have a reliable page count method
you must collect filename and page count by using a model like this:
public class PdfFileInfo
{
public string Filename { get; set; }
public int PageCount { get; set; }
}
e.g.
private void GetPdfFiles(string folder)
{
var pdfFileInfos = new List<PdfFileInfo>();
var filePaths = Directory.GetFiles(folder, "*.pdf", SearchOption.AllDirectories);
foreach (var filePath in filePaths)
{
pdfFileInfos.Add(new PdfFileInfo
{
Filename = filePath,
PageCount = GetNumberOfPdfPages(filePath)
});
}
pdfFileInfos = pdfFileInfos.OrderBy(x => x.PageCount).ToList();
if (pdfFileInfos.Count > 1)
{
var result = pdfFileInfos[0];
MessageBox.Show($"{result.Filename} has {result.PageCount} pages.");
}
}