2017-03-03 12 views
-1

FileSystemObject.CopyFile을 사용하는 데 문제가 있습니다. 에서 제안한 것처럼FileSystemObject CopyFile : 처리되지 않은 예외

Public Class Form1 
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim fso As New Scripting.FileSystemObject 
    Dim testfolderchk 
    testfolderchk = Dir("C:\Test\") 
    Dim inforeader As System.IO.FileInfo 
    Dim filedestinationcheck = Dir("C:\Test2\") 

    If testfolderchk <> "" Then 
     If Microsoft.VisualBasic.Left(testfolderchk, 4) = "test" Then 
      inforeader = My.Computer.FileSystem.GetFileInfo("C:\Test" & testfolderchk) 
      filetime = (inforeader.LastWriteTime) 
      If testfolderchk = filedestinationcheck Then GoTo skipfile 
      If testfolderchk = filedestinationcheck2 Then GoTo skipfile 

     Else : GoTo skipfile 
     End If 
    End If 

fso.CopyFile(testfolderchk, filedestinationcheck, True) 
+3

은'시스템을 무슨 뜻인지를 보여주기 위해 나는 코드의 비트를 초안 한

Copies an existing file to a new file, allowing the overwriting of an existing file.

: N 방법 CopyTo (String, Boolean)를 사용 .IO' 네임 스페이스는 'FileSystemObject'보다 NET 코드에 더 적합한 모든 종류의 파일 관련 메소드를 가지고 있습니다. – Plutonix

+0

제안 사항? 누군가? – user2644085

+0

'제안? '예, FSO를 사용하지 말고'GoTo'를 사용하지 마십시오. 그리고 약간의 연구만으로도 여기에서 파일 복사 애플릿 수백 개를 찾을 수 있습니다. – Plutonix

답변

2

: 나는 내가 읽은 포럼에서, 올바르게 사용하고 생각하지만, 난 여전히 다음과 같은 컴파일러 오류가 점점 오전 : 코드 여기

ArgumentException was unhandled: Value does not fall within expected range

입니다 FileInfo 당신이 캘리포니아로

Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects. This class cannot be inherited.

: 특별히 FileInfo 대신 System.IO 네임 스페이스를, FileSystemObject의 사용을 폐기하고 사용한다 코멘트

Dim folderToCheck As String = "C:\Test" 
Dim destinationFolder As String = "C:\Test2" 

Dim file As IO.FileInfo = New IO.FileInfo(IO.Path.Combine(folderToCheck, "test.txt")) 

Dim filetime As Date = file.LastWriteTime 

file.CopyTo(IO.Path.Combine(destinationFolder, file.Name), True) 

Path.Combine의 사용 :

Combines two strings into a path.

+1

감사합니다. 나는 비슷한 것을했고 그것은 훌륭하게 작동했다. 감사합니다. @Bugs – user2644085