Jerry Trac
Jerry Trac

Reputation: 367

File is a directory not a file

I have a folder called test which has subfolders: A, B, and C at the root. I am trying to copy a file to these three folders.

Not sure why the I am getting the error:

The target file "c:\test\A" is a directory and not a file. Please help.

 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

Upvotes: 0

Views: 560

Answers (1)

Steve
Steve

Reputation: 216291

You are passing a directory name to CopyTo.
That method wants a filename not a directory name.
Thus the exception received.

If I understand your code well, you need to change that line to

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

Also using DirectoryInfo objects is not really necessary here.
The simple Directory class could do the same things with less overhead

Upvotes: 1

Related Questions