반응형

* VBNET 폴더 복사 예제...

 

메인 화면

전체 소스 코드

Form1.vb

 

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        '원본 폴더 열기
        Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()

        If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtOriginFolder.Text = fbd.SelectedPath
        End If

    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        '복사될 대상 위치 폴더 열기
        Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()

        If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtCopyFolder.Text = fbd.SelectedPath
        End If

    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        '폴더 복사
        If txtOriginFolder.Text = "" Or txtCopyFolder.Text = "" Then
            MessageBox.Show("원본 폴더 또는 복사 대상 폴더를 선택해 주세요.", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Return
        End If

        button3.Text = "폴더 복사 중..."
        button3.Refresh()

        If FolderCopy(txtOriginFolder.Text, txtCopyFolder.Text) Then
            MessageBox.Show("폴더 복사가 완료 되었습니다.", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Else
            MessageBox.Show("폴더 복사가 실패 하였습니다.", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        button3.Text = "폴더 복사"

    End Sub

    Private Function FileCopy(ByVal strOriginFile As String, ByVal strCopyFile As String) As Boolean

        Dim fi As System.IO.FileInfo = New System.IO.FileInfo(strOriginFile)
        Dim lSize As Long = 0
        Dim lTotalSize As Long = fi.Length
        '버퍼 사이즈 임의 지정...
        Dim bBuf(1024) As Byte

        '복사될 파일이 존재 한다면 삭제하고...
        If System.IO.File.Exists(strCopyFile) Then
            System.IO.File.Delete(strCopyFile)
        End If

        '원본 파일 열기...
        Using fsIn As System.IO.FileStream = New System.IO.FileStream(strOriginFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
            '대상 파일 만들기...
            Using fsOut As System.IO.FileStream = New System.IO.FileStream(strCopyFile, IO.FileMode.Create, IO.FileAccess.Write)

                While (lSize < lTotalSize)
                    Try
                        Dim iLen As Integer = fsIn.Read(bBuf, 0, bBuf.Length)   '원본 파일 버퍼크기 만큼 읽기
                        lSize += iLen                                           '실제 원본 파일 읽어온 버퍼 갯수
                        fsOut.Write(bBuf, 0, iLen)                              '읽어온 버퍼만큼 대상 파일에 쓰기
                    Catch ex As Exception
                        fsOut.Flush()
                        fsOut.Close()
                        fsIn.Close()

                        '파일 복사 에러시 대상 파일은 일단 삭제...
                        If System.IO.File.Exists(strCopyFile) Then
                            System.IO.File.Delete(strCopyFile)
                        End If

                        Return False
                    End Try

                End While

                fsOut.Flush()
                fsOut.Close()
            End Using

            fsIn.Close()
        End Using

        Return True

    End Function

    Private Function FolderCopy(ByVal strOriginFolder As String, ByVal strCopyFolder As String) As Boolean
        '폴더가 없으면 만들기..
        If (Not System.IO.Directory.Exists(strCopyFolder)) Then
            System.IO.Directory.CreateDirectory(strCopyFolder)
        End If

        '파일 목록 불러오기...
        Dim strFiles() As String = System.IO.Directory.GetFiles(strOriginFolder)
        '폴더 목록 불러오기...
        Dim strFolders() As String = System.IO.Directory.GetDirectories(strOriginFolder)

        Try
            '파일 복사 부분...
            For Each strFile As String In strFiles
                Dim strName As String = System.IO.Path.GetFileName(strFile)
                Dim strDest As String = System.IO.Path.Combine(strCopyFolder, strName)

                '파일 복사
                FileCopy(strFile, strDest)

            Next

            For Each strFolder As String In strFolders
                Dim strName As String = System.IO.Path.GetFileName(strFolder)
                Dim strDest As String = System.IO.Path.Combine(strCopyFolder, strName)

                FolderCopy(strFolder, strDest)
            Next

        Catch ex As Exception
            Return False
        End Try
        
        Return True

    End Function

End Class

 

* 예제 결과

 

결과 화면

 

 

위 그림들은 테스트 결과 이미지들 입니다. 실제 복사가 정상적으로 이루어 졌는지 복사 하면서 잃게 된 용량은 없는지



https://kdsoft-zeros.tistory.com/50?category=846222

 

[VBNET] FileCopy (파일 복사 예제)

* VBNET 파일 복사 예제... 전체 소스 코드 Form1.vb Public Class Form1 '첫번째 방법... Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim..

kdsoft-zeros.tistory.com

https://kdsoft-zeros.tistory.com/40?category=846222

 

[VBNET] 폴더 및 파일, 드라이브 사이즈 (Size) 구하기

* 폴더 및 파일, 드라이브 목록 및 사이즈 구하기 예제 File Open 버튼 : 파일 대화 상자가 뜨게 되며, 해당 파일 선택 시 위 그림과 같이 파일의 위치 와 사이즈가 표시 됩니다. 다만 사이즈 표시는 기본 Byte..

kdsoft-zeros.tistory.com

 

반응형

+ Recent posts