我收到错误“System.Array 不包含 LastWriteTime 的定义”

发布于 2024-10-16 00:59:48 字数 925 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无名指的心愿 2024-10-23 00:59:48

你想做的大概就是这个?

        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);
            }
        }

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);
            }
        }
浮云落日 2024-10-23 00:59:48

好吧,fi 是一个数组,因此 fi.LastWriteTime.Date == DateTime.Today.Date 会给出该错误。正确。

您似乎没有将返回的目录用于其他任何用途,因此我无法建议“修复”。

Well, fi is an array so fi.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'.

说不完的你爱 2024-10-23 00:59:48

您正在对数组调用 LastWriteTime,而数组没有此属性。

您需要对数组的成员调用 LastWriteTime,例如

fi[0].LastWriteTime

或者迭代所有文件:

foreach(var file in fi) 
{
   if(file.LastWriteTime.Date == DateTime.Today.Date)
   {
       ....
   }
}

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.

fi[0].LastWriteTime

Or to iterate over all the files:

foreach(var file in fi) 
{
   if(file.LastWriteTime.Date == DateTime.Today.Date)
   {
       ....
   }
}
不醒的梦 2024-10-23 00:59:48

错误很简单,您在数组上使用 LastWriteTime 而不是 FileInfo 项。您应该在代码中使用索引,如下所示:

fi[0].LastWriteTime.Date  ///your code

将 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);
            }
}

Error is simple you are using LastWriteTime on array instead of the FileInfo item. 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:

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);
            }
}
如痴如狂 2024-10-23 00:59:48

如前所述,此属性在数组上无效。
只需用 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);
   }
 }

As previously stated, this Property is invalid on an array.
Just enclose your FileInfo with a foreach loop:

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);
   }
 }
Bonjour°[大白 2024-10-23 00:59:48

你需要

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);
    }
}
苏辞 2024-10-23 00:59:48

你错过了 for 循环:

 for (int i = 0; i < fi.Length; ++i)
 {
     if (fi[i].LastWriteTime.Date == DateTime.Today.Date)
     ...
 }

You are missinf a for loop:

 for (int i = 0; i < fi.Length; ++i)
 {
     if (fi[i].LastWriteTime.Date == DateTime.Today.Date)
     ...
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文