1

我想知道如何将一个文件夹中的文件从今天开始不到 2 天的时间复制到另一个文件夹。

这就是我所拥有的:

Dim source As New DirectoryInfo("C:\Users\username\Desktop\123")
Dim target As New DirectoryInfo("C:\Users\username\Desktop\345")
Dim files = source.GetFiles("*", SearchOption.AllDirectories).Where(Function(file) file.LastWriteTime.Date.Equals(Date.Today.AddDays(-2)))
          For Each file As FileInfo In files
            file.copy(file.FullName, target.FullName & "\" & file.Name, True)
        Next file

编辑:

错误截图:

在此处输入图像描述

4

2 回答 2

1

第一个问题:在 For 循环中,您正在分配一个变量名称file,并且与具有相同名称的 Class 冲突System.IO.File,因此您需要更改变量名称,指定Copy方法的 Class。

第二个问题:该Copy方法是System.IO.FileClass的成员,而不是System.IO.FileInfo仅是一组用于检索文件信息的方法和属性,并且在For循环中您试图CopyFileInfo对象中访问该成员,但该成员不存在。

那么你的代码应该是这样的:

Imports System.IO

Public Class Form1

    Private Sub Test() Handles MyBase.Load

        Dim Source As New DirectoryInfo("C:\Users\username\Desktop\123")
        Dim Target As New DirectoryInfo("C:\Users\username\Desktop\345")

        Dim Files As FileInfo() =
            (From [File] As FileInfo In Source.GetFiles("*", SearchOption.AllDirectories)
             Where [File].LastWriteTime.Date.Equals(Date.Today.AddDays(-2))).ToArray

        For Each [File] As FileInfo In Files
            IO.File.Copy([File].FullName, 
                         Path.Combine(Target.FullName, [File].Name), True)
        Next [File]

    End Sub

End Class
于 2014-02-26T08:45:29.183 回答
0
Dim source As New DirectoryInfo("C:\Users\username\Desktop\123")
Dim target As New DirectoryInfo("C:\Users\username\Desktop\345")
Dim ChildFile As FileInfo
For Each ChildFile In source.GetFiles("*", SearchOption.AllDirectories).Where(Function(file) file.LastWriteTime.Date.Equals(Date.Today.AddDays(-2)))
      ChildFile.CopyTo(Path.Combine(target.FullName, ChildFile.Name), False)
Next
于 2014-02-26T07:40:46.893 回答