[英]C# GetFiles with extension and date filter
I'm looking to only return .xls files that were created today from a folder containing multiple files and a range of dates. 我只想返回今天从包含多个文件和一个日期范围的文件夹中创建的.xls文件。
I'm trying this: 我正在尝试:
FBD.SelectedPath = @"\\USMCO\Test\";
string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls")
.Where(file => new FileInfo(file).CreationTime.Date == DateTime.Today.Date);
foreach (string file in files)
{
listBox1.Items.Add(Path.GetFileName(file));
}
But get this error. 但是得到这个错误。 CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string[]'. CS0266无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'string []'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)
What am I missing here? 我在这里想念什么? TIA TIA
Try this you were missing to call .ToArray() Method 尝试此操作,您无法调用.ToArray()方法
string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls")
.Where(file => new FileInfo(file).CreationTime.Date == DateTime.Today.Date).ToArray();
Alternate way 替代方式
foreach (string file in Directory.GetFiles("", "*.xls").Where(file =>
new FileInfo(file).CreationTime.Date == DateTime.Today.Date))
{
listBox1.Items.Add(Path.GetFileName(file));
}
Convert the enumerable to array by calling its ToArray method: 通过调用其ToArray方法将可枚举转换为数组:
string[] files = Directory.GetFiles(FBD.SelectedPath, "*.xls").Where(file =>
new FileInfo(file).CreationTime.Date == DateTime.Today.Date).ToArray();
The Where
extension method returns an IEnumerable
, not an array. Where
扩展方法返回IEnumerable
,而不是数组。 But you don't need an array to execute a foreach
loop, you can simply change string[] files = Directory....
to var files = Directory....
and continue with the rest of the code as is. 但是您不需要数组来执行foreach
循环,您只需将string[] files = Directory....
更改为var files = Directory....
然后继续其余代码即可。
FBD.SelectedPath = @"\\USMCO\Test\";
var files = Directory.GetFiles(FBD.SelectedPath, "*.xls")
.Where(file => new FileInfo(file).CreationTime.Date == DateTime.Today.Date);
foreach (string file in files)
{
listBox1.Items.Add(Path.GetFileName(file));
}
However, this can be done better, using DirectoryInfo.EnumerateFiles
instead of Directory.GetFiles
, you can get the FileInfo
directly instead of having to create one for each file: 但是,使用DirectoryInfo.EnumerateFiles
代替Directory.GetFiles
可以做得更好,您可以直接获取FileInfo
,而不必为每个文件创建一个:
FBD.SelectedPath = @"\\USMCO\Test\";
var files = DirectoryInfo.EnumerateFiles(FBD.SelectedPath, "*.xls")
.Where(file => new file.CreationTime.Date == DateTime.Today.Date);
// file here is an instance of FileInfo!
foreach (var file in files)
{
// FileInfo.Name contains only the file name, not the entire path
listBox1.Items.Add(file.Name);
}
You forgot to select file names: 您忘记选择文件名:
string[] files = Directory
.GetFiles(FBD.SelectedPath, "*.xls")
.Select(path => new FileInfo(path)
.Where(file => file.CreationTime.Date == DateTime.Today.Date)
.Select(file => file.FullName)
.ToArray();
Alternative solution: 替代解决方案:
string[] files = new DirectoryInfo(FBD.SelectedPath)
.GetFiles("*.xls")
.Where(file => file.CreationTime.Date == DateTime.Today.Date)
.Select(file => file.FullName)
.ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.