我收到错误“System.Array 不包含 LastWriteTime 的定义”
我的 c# 代码有问题,我似乎无法让 if 语句工作,我认为它出于某种原因试图引用代码的错误部分。我已经检查了我是否拥有所有正确的引用和所有正确的用途。我已粘贴了以下有问题的代码:
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);
}
}
任何可以提供的帮助将不胜感激。
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你想做的大概就是这个?
What you want to do is probably this?
好吧,fi 是一个数组,因此 fi.LastWriteTime.Date == DateTime.Today.Date 会给出该错误。正确。
您似乎没有将返回的目录用于其他任何用途,因此我无法建议“修复”。
Well,
fi
is an array sofi.LastWriteTime.Date == DateTime.Today.Date
would give that error. Correctly.You don't seem to use the returned directories for anything else, so I'm not able to suggest a 'fix'.
您正在对数组调用 LastWriteTime,而数组没有此属性。
您需要对数组的成员调用 LastWriteTime,例如
或者迭代所有文件:
You are calling LastWriteTime on the array, and an array doesn't have this property.
You need to call LastWriteTime on the members of the array, e.g.
Or to iterate over all the files:
错误很简单,您在数组上使用
LastWriteTime
而不是 FileInfo 项。您应该在代码中使用索引,如下所示:将 0 替换为索引号,或者在 foreach 循环中使用它,如下所示:
Error is simple you are using
LastWriteTime
on array instead of the FileInfo item. You should use an index in the code like this:replace 0 with your index num or use it in a foreach loop like this:
如前所述,此属性在数组上无效。
只需用 foreach 循环将 FileInfo 括起来:
As previously stated, this Property is invalid on an array.
Just enclose your FileInfo with a foreach loop:
你需要
You need
你错过了 for 循环:
You are missinf a for loop: