[英]i get an error “System.Array does not contain a definition for LastWriteTime”
I am having a problem with my c# code I can't seem to get my if statement to work I think it is trying to reference the wrong part of code for some reason. 我的C#代码有问题,我似乎无法使我的if语句正常工作,我认为由于某种原因它试图引用错误的代码部分。 I have checked that I have all the right references in and all the right uses in. I have pasted the offending code bellow:
我检查了一下是否具有所有正确的引用以及所有正确的用法。我已经粘贴了下面的令人讨厌的代码:
FolderBrowserDialog dlg2 = new FolderBrowserDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
//do whatever with dlg.SelectedPath
{
string searchPattern = "*";
DirectoryInfo source = new DirectoryInfo(dlg.SelectedPath);
DirectoryInfo target = new DirectoryInfo(dlg2.SelectedPath);
DirectoryInfo dir = new DirectoryInfo(dlg.SelectedPath);
FileInfo[] fi = dir.GetFiles("*", SearchOption.AllDirectories);
{
if (fi.LastWriteTime.Date == DateTime.Today.Date)
{
FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
for (int i = 0; i < sourceFiles.Length; ++i)
File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
}
}
any help that could be given would be gratefully appreciated thanks. 非常感谢您所能提供的任何帮助。
What you want to do is probably this? 您想做的可能是这个吗?
FileInfo[] fis = dir.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo fi in fis)
{
if (fi.LastWriteTime.Date == DateTime.Today.Date)
{
FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
for (int i = 0; i < sourceFiles.Length; ++i)
File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
}
}
Well, fi
is an array so fi.LastWriteTime.Date == DateTime.Today.Date
would give that error. 好吧,
fi
是一个数组,所以fi.LastWriteTime.Date == DateTime.Today.Date
会给出该错误。 Correctly. 正确地。
You don't seem to use the returned directories for anything else, so I'm not able to suggest a 'fix'. 您似乎没有将返回的目录用于其他任何内容,因此我无法建议“修复”。
You are calling LastWriteTime on the array, and an array doesn't have this property. 您正在数组上调用LastWriteTime,而数组没有此属性。
You need to call LastWriteTime on the members of the array, eg 您需要在数组的成员上调用LastWriteTime,例如
fi[0].LastWriteTime
Or to iterate over all the files: 或遍历所有文件:
foreach(var file in fi)
{
if(file.LastWriteTime.Date == DateTime.Today.Date)
{
....
}
}
Error is simple you are using LastWriteTime
on array instead of the FileInfo item. 错误很简单,您在数组而不是FileInfo项上使用
LastWriteTime
。 You should use an index in the code like this: 您应该在以下代码中使用索引:
fi[0].LastWriteTime.Date ///your code
replace 0 with your index num or use it in a foreach loop like this: 用您的索引num替换0或在这样的foreach循环中使用它:
foreach(var item in fi)
{
if (item.LastWriteTime.Date == DateTime.Today.Date)
{
FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
for (int i = 0; i < sourceFiles.Length; ++i)
File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
}
}
As previously stated, this Property is invalid on an array. 如前所述,此属性在数组上无效。 Just enclose your FileInfo with a foreach loop:
只需用foreach循环将FileInfo括起来:
foreach(FileInfo fi in dir.GetFiles("*", SearchOption.AllDirectories))
{
if (fi.LastWriteTime.Date == DateTime.Today.Date)
{
FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
for (int i = 0; i < sourceFiles.Length; ++i)
File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
}
}
You need 你需要
foreach(FileInfo fi in dir.GetFiles("*", SearchOption.AllDirectories))
{
if (fi.LastWriteTime.Date == DateTime.Today.Date)
{
FileInfo[] sourceFiles = source.GetFiles(searchPattern, SearchOption.AllDirectories);
for (int i = 0; i < sourceFiles.Length; ++i)
File.Copy(sourceFiles[i].FullName, target.FullName + "\\" + sourceFiles[i].Name, true);
}
}
You are missinf a for loop: 您错过了for循环:
for (int i = 0; i < fi.Length; ++i)
{
if (fi[i].LastWriteTime.Date == DateTime.Today.Date)
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.