2012-02-09 3 views
6

폴더가없는 경우 해당 폴더를 만들어서 한 디렉터리에서 다른 디렉터리로 파일을 복사 할 때 문제가 있습니다. 대상 디렉토리.폴더가 존재하지 않는 경우 해당 폴더를 생성하여 한 디렉터리에서 다른 디렉터리로 파일을 복사하는 방법

예 :

  • 소스 경로 : C:\temp\test\1.txt
  • 대상 경로 : C:\Data\

C:\Data\ 경우는 "임시"또는 "테스트"폴더를 포함하지 않는, 그것은 폴더 이전을 만들어야합니다 대처 1.txt. C:\Data\temp\test\1.txt

에 복사

다음은 내 코드입니다. 하지만 작동하지 않습니다 ..

Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click 
      Dim sourcepath As String = "C:\temp\test\1.txt" 
    Dim DestPath As String = "C:\Data\" 
    CopyDirectory(sourcepath, DestPath) 
End Sub 

Private Shared Sub CopyDirectory(sourcePath As String, destPath As String) 
    If Not Directory.Exists(destPath) Then 
     Directory.CreateDirectory(destPath) 
    End If 

    For Each file__1 As String In Directory.GetFiles(sourcePath) 
     Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1)) 
     File.Copy(file__1, dest) 
    Next 

    For Each folder As String In Directory.GetDirectories(sourcePath) 
     Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder)) 
     CopyDirectory(folder, dest) 
    Next 
End Sub 
+0

오류를

그래서 단순히 솔루션은 디렉토리 이름에서 1.txt을 제거하는 것입니다? 그리고 당신은 XP 또는 Windows 7을 사용하고 있습니까? –

+4

"작동하지 않습니다"와 같은 문구는 질문을 제출할 때 SO에 의해 차단되어야합니다.) –

답변

9

다음은 디렉토리가 아닙니다.

Dim sourcepath As String = "C:\temp\test\1.txt" 

당신은 Directory.GetFiles(sourcePath)의 디렉토리로 사용하고 있기 때문에.

그 외에도 다음 번에 질문을 자세히 작성하는 것이 좋습니다. 이 코드는 DirectoryNotFoundException과 같은 의미있는 예외를 메시지 또는 파일이있는 경우 IOException 메시지가 인 "디렉터리 이름이 잘못되었습니다"과 같이 표시합니다. 질문에 추가 했어야합니다. 당신은 하나의 파일을 복사해야하는 경우,

Dim sourcepath As String = "C:\temp\test\" 

CopyTo method 사용 :

Dim sourcepath As String = "C:\temp\test\" 
Dim DestPath As String = "C:\temp\Data\" 
If Not Directory.Exists(DestPath) Then 
    Directory.CreateDirectory(DestPath) 
End If 
Dim file = New FileInfo("C:\temp\test\1.txt") 
file.CopyTo(Path.Combine(DestPath, file.Name), True) 
+0

텍스트 파일이 많지만 1.txt 파일 만 복사하려고합니다. – user1101157

+0

@ user1101157 : 내 대답이 업데이트되었습니다. –

0
Dim strMasterResourceDirectory As String 
    Dim strDirectory As String 

    strDirectory = "C:\TestDestination" 
    strMasterResourceDirectory = "TestResource" 

    If My.Computer.FileSystem.DirectoryExists(strDirectory) = False Then 
     My.Computer.FileSystem.CreateDirectory(strDirectory) 
    End If 

    ' Loop through each file in the directory 
    For Each file As IO.FileInfo In New IO.DirectoryInfo(strDirectory).GetFiles 

     If file.Name <> "Thumbs.db" Then 

      System.IO.File.Delete(strDirectory & "\" & file.Name) 

     End If 
    Next 

    ' Loop through each file in the directory 
    For Each file As IO.FileInfo In New IO.DirectoryInfo(strMasterResourceDirectory).GetFiles 

     If file.Name <> "Thumbs.db" Then 

      ' copy resource to users local directory 

      file.CopyTo(strDirectory & "\" & file.Name) 

     End If 
    Next 
+0

StackOverflow에 오신 것을 환영합니다! 코드에 약간의 설명을 추가하십시오. 고맙습니다! – Aurasphere

관련 문제