2013-04-10 111 views
1

我有一個文件夾稱爲測試,其中有子文件夾:A,B和C在根。我正在嘗試將文件複製到這三個文件夾。文件是一個目錄而不是文件

不知道爲什麼我收到錯誤:

目標文件「C:\測試\ 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

您正將一個目錄名稱傳遞給CopyTo
該方法需要的文件名不是目錄名稱。
因此收到異常。

如果我理解你的代碼好,你需要到該行改變

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

而且使用的DirectoryInfo對象來這裏是不是真的有必要。
簡單的Directory類可以用較少的開銷做同樣的事情

相關問題