All pastes #189154 Raw Edit

nox23

public text v1 · immutable
·published 2006-10-02 21:33 UTC
rendered paste body
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Code généré par le Concepteur Windows Form "

    Public Sub New()
        MyBase.New()

        'Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        'Ajoutez une initialisation quelconque après l'appel InitializeComponent()

    End Sub

    'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Requis par le Concepteur Windows Form
    Private components As System.ComponentModel.IContainer

    'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form
    'Elle peut être modifiée en utilisant le Concepteur Windows Form.  
    'Ne la modifiez pas en utilisant l'éditeur de code.
    Friend WithEvents txtSource As System.Windows.Forms.TextBox
    Friend WithEvents txtDestination As System.Windows.Forms.TextBox
    Friend WithEvents cmdStart As System.Windows.Forms.Button
    Friend WithEvents FBD1 As System.Windows.Forms.FolderBrowserDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtSource = New System.Windows.Forms.TextBox
        Me.txtDestination = New System.Windows.Forms.TextBox
        Me.cmdStart = New System.Windows.Forms.Button
        Me.FBD1 = New System.Windows.Forms.FolderBrowserDialog
        Me.SuspendLayout()
        '
        'txtSource
        '
        Me.txtSource.BackColor = System.Drawing.Color.Red
        Me.txtSource.Location = New System.Drawing.Point(16, 16)
        Me.txtSource.Name = "txtSource"
        Me.txtSource.ReadOnly = True
        Me.txtSource.Size = New System.Drawing.Size(368, 20)
        Me.txtSource.TabIndex = 0
        Me.txtSource.Text = ""
        '
        'txtDestination
        '
        Me.txtDestination.BackColor = System.Drawing.Color.Red
        Me.txtDestination.Location = New System.Drawing.Point(16, 40)
        Me.txtDestination.Name = "txtDestination"
        Me.txtDestination.ReadOnly = True
        Me.txtDestination.Size = New System.Drawing.Size(368, 20)
        Me.txtDestination.TabIndex = 1
        Me.txtDestination.Text = ""
        '
        'cmdStart
        '
        Me.cmdStart.Location = New System.Drawing.Point(16, 72)
        Me.cmdStart.Name = "cmdStart"
        Me.cmdStart.Size = New System.Drawing.Size(368, 24)
        Me.cmdStart.TabIndex = 2
        Me.cmdStart.Text = "Start backup !"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(408, 301)
        Me.Controls.Add(Me.cmdStart)
        Me.Controls.Add(Me.txtDestination)
        Me.Controls.Add(Me.txtSource)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub txtSource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSource.Click
        If FBD1.ShowDialog = DialogResult.Cancel Then
            txtSource.Text = ""
            txtSource.BackColor = Color.Red
        Else
            txtSource.Text = FBD1.SelectedPath
            txtSource.BackColor = Color.Green
        End If
    End Sub
    Private Sub txtDestination_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDestination.Click
        If FBD1.ShowDialog = DialogResult.Cancel Then
            txtDestination.Text = ""
            txtDestination.BackColor = Color.Red
        Else
            txtDestination.Text = FBD1.SelectedPath
            txtDestination.BackColor = Color.Green
        End If
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        If txtDestination.Text = "" And txtSource.Text = "" Then Return
        RecursiveCopyFiles(txtSource.Text, txtDestination.Text, True, True)
    End Sub
    Public Sub RecursiveCopyFiles(ByVal sourceDir As String, _
                ByVal destDir As String, _
                ByVal fRecursive As Boolean, ByVal overWrite As Boolean)

        Dim i As Integer
        Dim posSep As Integer
        Dim sDir As String
        Dim aDirs() As String
        Dim sFile As String
        Dim aFiles() As String

        If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            sourceDir &= System.IO.Path.DirectorySeparatorChar
        End If

        If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            destDir &= System.IO.Path.DirectorySeparatorChar
        End If

        If fRecursive Then
            aDirs = System.IO.Directory.GetDirectories(sourceDir)

            For i = 0 To aDirs.GetUpperBound(0)
                posSep = aDirs(i).LastIndexOf("\")
                sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length - (posSep + 1))
                System.IO.Directory.CreateDirectory(destDir + sDir)
                RecursiveCopyFiles(aDirs(i), (destDir + sDir), fRecursive, overWrite)
            Next

        End If
        aFiles = System.IO.Directory.GetFiles(sourceDir)

        For i = 0 To aFiles.GetUpperBound(0)

            posSep = aFiles(i).LastIndexOf("\")

            sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length - (posSep + 1))
            Try

                System.IO.File.Copy(aFiles(i), destDir + sFile, False)
            Catch ex As Exception
                If overWrite = False Then
                    MsgBox(ex.Message)
                Else
                    System.IO.File.Copy(aFiles(i), destDir + sFile, True)
                End If

            End Try
        Next i

    End Sub
End Class