Imports System.Windows.Forms Imports System.IO ' Public Class DotNetCopy ' #Region "Public Events" ' Public Event Start(ByVal sender As Object, ByVal e As System.EventArgs) ' Public Event Terminate(ByVal sender As Object, ByVal e As System.EventArgs) ' Public Event Progress(ByVal sender As Object, ByVal e As CopyFileProgressEventsArgs) ' Public Event [Error](ByVal sender As Object, ByVal e As CopyFileErrorEventsArgs) ' #End Region ' #Region "Private Fields" ' Private _BufferSize As Integer = 8192 ' Private _bCancel As Boolean ' #End Region ' #Region "Public Propertys" ' Property BufferSize() As Integer Get Return _BufferSize End Get Set(ByVal value As Integer) _BufferSize = value End Set End Property ' #End Region ' #Region "Public Methods" ' Public Sub Cancel() ' _bCancel = True '' End Sub ' Public Function CopyFile(ByVal Source As String, ByVal Dest As String, Optional ByVal Overwrite As Boolean = False) As Boolean ' _bCancel = False ' RaiseEvent Start(Me, New System.EventArgs) ' If File.Exists(Dest) Then If Overwrite Then Try ' File.Delete(Dest) ' Catch ex As Exception ' RaiseEvent [Error](Me, New CopyFileErrorEventsArgs(ex)) Return False ' End Try Else ' Return False ' End If End If ' Dim fs_Source As IO.FileStream = Nothing Dim fs_Cible As IO.FileStream = Nothing ' Dim SR As BinaryReader = Nothing Dim SW As BinaryWriter = Nothing ' Try ' fs_Source = New FileStream(Source, FileMode.Open) fs_Cible = New FileStream(Dest, FileMode.Create) ' Dim FileLenght As Long = fs_Source.Length ' SR = New BinaryReader(fs_Source) SW = New BinaryWriter(fs_Cible) ' Dim Buffer As Byte() SR.ReadBytes(BufferSize), BytesWriten As Long 0 ' While Buffer.Length > 0 ' SW.Write(Buffer) ' BytesWriten += Buffer.Length Dim Prc As Integer = Math.Round((BytesWriten / FileLenght) * 100) RaiseEvent Progress(Me, New CopyFileProgressEventsArgs(Prc)) ' If _bCancel Then ' _bCancel = False ' SR.Close() SW.Close() ' Try ' IO.File.Delete(Dest) ' Catch ex As Exception ' End Try ' RaiseEvent Terminate(Me, New System.EventArgs) Return False ' End If ' Application.DoEvents() ' Buffer = SR.ReadBytes(BufferSize) ' End While ' RaiseEvent Terminate(Me, New System.EventArgs) Return True ' Catch ex As IOException ' RaiseEvent [Error](Me, New CopyFileErrorEventsArgs(ex)) Return False ' Finally If fs_Cible IsNot Nothing AndAlso _ fs_Cible.CanRead Then fs_Cible.Close() fs_Cible.Dispose() End If If fs_Source IsNot Nothing AndAlso _ fs_Source.CanRead Then fs_Source.Close() fs_Source.Dispose() End If End Try '' End Function ' #End Region ' End Class ' Public Class CopyFileErrorEventsArgs Inherits System.EventArgs ' #Region "Ctor" ' Sub New() ' End Sub Sub New(ByVal Err As Exception) ' _Error = Err '' End Sub ' #End Region ' #Region "Private Fields" ' Private _Error As Exception ' #End Region ' #Region "Public Propertys" ' Property [Error]() As Exception Get Return _Error End Get Set(ByVal value As Exception) _Error = value End Set End Property ' #End Region ' End Class ' Public Class CopyFileProgressEventsArgs Inherits System.EventArgs ' #Region "Ctor" ' Sub New() End Sub Sub New(ByVal Progress As Integer) ' _Progress = Progress '' End Sub ' #End Region ' #Region "Private Fields" ' Private _Progress As Integer ' #End Region ' #Region "Public Propertys" ' Property Progress() As Integer Get Return _Progress End Get Set(ByVal value As Integer) _Progress = value End Set End Property ' #End Region ' End Class
Private WithEvents DNCopy As New DotNetCopy ' Sub Start(ByVal sender As Object, ByVal e As System.EventArgs) Handles DNCopy.Start End Sub ' Sub Progress(ByVal sender As Object, ByVal e As CopyFileProgressEventsArgs) Handles DNCopy.Progress pg.Value = e.Progress End Sub ' Sub [Error](ByVal sender As Object, ByVal e As CopyFileErrorEventsArgs) Handles DNCopy.Error MsgBox(e.Error.Message) End Sub ' Sub Terminate(ByVal sender As Object, ByVal e As System.EventArgs) Handles DNCopy.Terminate End Sub ' Private Sub cmd_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cancel.Click DNCopy.Cancel() End Sub ' Private Sub btn_Copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Copy.Click DNCopy.CopyFile("c:\test.mkv", "d:\test.mkv", True) End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question