0

我有一个名为 test 的文件夹,其中有子文件夹:根目录下的 A、B 和 C。我正在尝试将文件复制到这三个文件夹中。

不知道为什么我收到错误:

目标文件“c:\test\A”是一个目录而不是一个文件。请帮忙。

 Dim OPUSINI As New FileInfo("C:\Program Files (x86)\OPUS_4.5\OPUS32.INI")
    'Where is will be going
    'Dim Win7DestLocation As String = "C:\Users"
    Dim Win7DestLocation As String = "C:\test"
    Dim WinXPDestLocation As String = "C:\Documents and Settings"
    'Get a list of all the Subfolders within the Destination location
    Dim Win7Destdir As New DirectoryInfo(Win7DestLocation)
    Dim WinXPDestdir As New DirectoryInfo(WinXPDestLocation)
    'Checks if Destination Exists for Windows 7
    Dim Win7CheckExistDestLocation As New IO.DirectoryInfo(Win7DestLocation)
    'Checks if Destination Exists for Windows XP
    Dim WinXPCheckExistDestLocation As New IO.DirectoryInfo(WinXPDestLocation)
    If Win7CheckExistDestLocation.Exists Then
        Try
            For Each subfolder As DirectoryInfo In Win7Destdir.GetDirectories


                OPUSINI.CopyTo(subfolder.FullName, True)
            Next
        Catch ex As Exception
            MessageBox.Show("Unable to backup Pump data files." + ex.ToString, "Backup Error:", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
4

1 回答 1

1

您正在将目录名称传递给CopyTo
该方法需要文件名而不是目录名。
因此收到异常。

如果我很好理解您的代码,您需要将该行更改为

Dim destFile = Path.Combine(subfolder.FullName, OPUSINI.Name))
OPUSINI.CopyTo(destFile, True)

这里也不需要使用 DirectoryInfo 对象。
简单的Directory类可以用更少的开销做同样的事情

于 2013-04-10T20:51:48.817 回答