文件是目录而不是文件

时间:2013-04-10 19:40:47

标签: vb.net

我有一个名为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

1 个答案:

答案 0 :(得分:1)

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

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

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

此处也不一定非常需要使用DirectoryInfo对象 简单的Directory类可以用更少的开销做同样的事情