"Michael" <mi*******@gmai l.comwrote in message
news:11******** **************@ w3g2000hsg.goog legroups.com...
>I need to copy a huge file (around 300Mb) from a mapped network drive
to another.
I have created a console application and used System.IO.File. Copy
function.
But I want to know the process of this copying work.
Is there any way?
I am thinking that I can get the file size, but I don't know how to
get the size copied.
Thanks.
Michael
Couple of links:
http://vbcity.com/forums/topic.asp?tid=36364
and a more verbose solution from Experts Exchange (good idea to join, these
guys are brilliant):
Option Explicit On Option Strict On
Imports System
Imports System.Net
Imports System.Text
Public Class DownloadWorker
Private _size As Long
Private mRead As Long
Private _status As DownLoadStatus
Private _errorDescripti on As String
Private _sourceURL As String
Private _destPath As String
Private _referer As String
Public Sub New()
MyBase.new()
_status = DownLoadStatus. Idle
End Sub
Public Sub New(ByVal sSourceURL As String, ByVal sDestPath As String)
MyBase.new()
_sourceURL = sSourceURL
_destPath = sDestPath
End Sub
Public Sub New(ByVal sSourceURL As String, ByVal sDestPath As String,
ByVal sReferer As String)
MyBase.new()
_sourceURL = sSourceURL
_destPath = sDestPath
_referer = sReferer
End Sub
Public Enum DownLoadStatus
Idle = 0
Connecting = 1
Connected = 2
Downloading = 3
Completed = 4
ErrorOccured = 5
End Enum
Public Event StatusChanged(B yRef sender As DownloadWorker, ByVal
OldStatus As DownLoadStatus, ByVal NewStatus As DownLoadStatus)
Public Event ProgressChanged (ByRef sender As DownloadWorker)
Public Property SourceURL() As String
Get
Return _sourceURL
End Get
Set(ByVal Value As String)
Select Case _status
Case DownLoadStatus. Connected, DownLoadStatus. Connecting,
DownLoadStatus. Downloading
Throw New InvalidOperatio nException("Sou rceURL cannot be
changed while download is in progress")
Case Else
_sourceURL = Value
End Select
End Set
End Property
Public Property DestPath() As String
Get
Return _destPath
End Get
Set(ByVal Value As String)
Select Case _status
Case DownLoadStatus. Connected, DownLoadStatus. Connecting,
DownLoadStatus. Downloading
Throw New InvalidOperatio nException("Des tination Path
cannot be changed while download is in progress")
Case Else
_destPath = Value
End Select
End Set
End Property
Public Property Referer() As String
Get
Return _referer
End Get
Set(ByVal Value As String)
Select Case _status
Case DownLoadStatus. Connected, DownLoadStatus. Connecting,
DownLoadStatus. Downloading
Throw New InvalidOperatio nException("Ref erer cannot be
changed while download is in progress")
Case Else
_referer = Value
End Select
End Set
End Property
Public ReadOnly Property Status() As DownLoadStatus
Get
Return _status
End Get
End Property
Public ReadOnly Property Progress() As Double
Get
If _size = 0 Then
Return 0
Else
Return mRead / _size
End If
End Get
End Property
Public ReadOnly Property Size() As Long
Get
Return _size
End Get
End Property
Public ReadOnly Property Downloaded() As Long
Get
Return mRead
End Get
End Property
Public ReadOnly Property ErrorDescriptio n() As String
Get
Return _errorDescripti on
End Get
End Property
Private Sub ChangeStatus(By Val NewStatus As DownLoadStatus)
Dim Temp As DownLoadStatus
Temp = _status
_status = NewStatus
RaiseEvent StatusChanged(M e, Temp, NewStatus)
End Sub
Public Sub DownloadFile()
Dim bBuffer() As Byte
Const BlockSize As Integer = 4096
Dim iRead As Integer
Dim iReadTotal As Integer
Dim iTotalSize As Integer
If _sourceURL = "" Then
Throw New InvalidOperatio nException("No Source URL specified")
Exit Sub
End If
If _destPath = "" Then
Throw New InvalidOperatio nException("No Destination Path
specified")
Exit Sub
End If
Try
Call ChangeStatus(Do wnLoadStatus.Co nnecting)
Dim wr As HttpWebRequest = CType(WebReques t.Create(_sourc eURL),
HttpWebRequest)
If _referer <"" Then
wr.Referer = _referer
End If
Dim resp As HttpWebResponse = CType(wr.GetRes ponse(),
HttpWebResponse )
_size = resp.ContentLen gth
Call ChangeStatus(Do wnLoadStatus.Co nnected)
Dim sIn As IO.Stream = resp.GetRespons eStream
Dim sOut As New IO.FileStream(_ destPath, IO.FileMode.Cre ate)
ReDim bBuffer(BlockSi ze - 1)
Call ChangeStatus(Do wnLoadStatus.Do wnloading)
iRead = sIn.Read(bBuffe r, 0, BlockSize)
mRead = iRead
While iRead 0
RaiseEvent ProgressChanged (Me)
sOut.Write(bBuf fer, 0, iRead)
iRead = sIn.Read(bBuffe r, 0, BlockSize)
mRead += iRead
End While
sIn.Close()
sOut.Close()
Call ChangeStatus(Do wnLoadStatus.Co mpleted)
Catch ex As Exception
_errorDescripti on = ex.Message
Call ChangeStatus(Do wnLoadStatus.Er rorOccured)
End Try
End Sub
End Class