1_091010151359_1  

Imports System.IO

Public Class AppUpdater

    Private Total As Integer = 0

    Private FileNumber As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Me.ProgressBar1.Maximum = Directory.CreateDirectory(Me.TextBox1.Text).

        Dim DirInfo_source As New DirectoryInfo(Me.Tbx_Source.Text)

        Total = 0

        FileNumber = Me.GetFilesCount(DirInfo_source)

        Me.ProgressBar1.Maximum = FileNumber

        Me.CopyDirector(Me.Tbx_Source.Text, Me.Tbx_Dest.Text)

    End Sub

    Public Sub CopyDirector(ByVal SourceDirName As String, ByVal DestdirName As String)

        If (Not Directory.Exists(DestdirName)) Then

            Directory.CreateDirectory(DestdirName)

            File.SetAttributes(DestdirName, File.GetAttributes(SourceDirName))

        End If

        If (DestdirName(DestdirName.Length - 1) <> Path.DirectorySeparatorChar) Then

            DestdirName = DestdirName + Path.DirectorySeparatorChar

            Dim files As String() = Directory.GetFiles(SourceDirName)

            For Each l_file As String In files

                File.Copy(l_file, DestdirName + Path.GetFileName(l_file), True)

                File.SetAttributes(DestdirName + Path.GetFileName(l_file), FileAttributes.Normal)

                Total = Total + 1

                Me.lblStatus.BackColor = Me.ProgressBar1.BackColor

                Me.ProgressBar1.Value = Total

                If (FileNumber = 0) Then

                    lblStatus.Text = "已完成  100%"

                Else

                    lblStatus.Text = "已完成  " + (Math.Round(CType((100 * Total / FileNumber), System.Double), 0)).ToString() + "%"

                End If

                Me.Label_From.Text = l_file

                Me.Label_To.Text = DestdirName + Path.GetFileName(l_file)

                lblStatus.Refresh()

                Label_From.Refresh()

                Label_To.Refresh()

            Next

            Dim dirs As String() = Directory.GetDirectories(SourceDirName)

            For Each dir As String In dirs

                Me.Refresh()

                Me.Focus()

                Me.Activate()

                CopyDirector(dir, DestdirName + Path.GetFileName(dir))

            Next

        End If

    End Sub

 

    Public Function GetFilesCount(ByVal dirInfo As System.IO.DirectoryInfo) As String

        Dim totalFile As Integer = 0

        totalFile += dirInfo.GetFiles().Length

        For Each subdir As System.IO.DirectoryInfo In dirInfo.GetDirectories

            totalFile += GetFilesCount(subdir)

        Next

        Return totalFile

    End Function

End Class

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()