[英]How to count number of Excel files from a folder using c#?
I need to count the number of excel files,pdf files from a directory.我需要计算目录中 excel 文件、pdf 文件的数量。
I have Counted the total number of files from a directory using我已经计算了一个目录中的文件总数
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"D:\");
int count = dir.GetFiles().Length;
Any Suggestion?有什么建议吗?
Here's a LINQ solution.这是 LINQ 解决方案。
var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".xls",
".xlsx",
".pdf",
};
var baseDir = @"D:\";
var count = Directory.EnumerateFiles(baseDir)
.Count(filename =>
extensions.Contains(Path.GetExtension(filename)));
Use SearchPattern in GetFiles
method.在GetFiles
方法中使用 SearchPattern。
dir.GetFiles("*.XLS");
int count = 0;
foreach (string file in Directory.GetFiles(@"D:\"))
{
if (file.EndsWith(".pdf") || file.EndsWith(".xls"))
{
count++;
}
}
var count = System.IO.Directory.GetFiles(@"D:\")
.Count(p => Path.GetExtension(p) == ".xls");
String[] excelFiles=Directory.GetFiles("C:\\", "*.xls");
int count = Directory.GetFiles(path).Count(f =>(f.EndsWith(".xls") || f.EndsWith(".xlsx")));
simple简单的
int count = dir.GetFiles("*.txt").Length + dir.GetFiles("*.pdf").Length
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.