473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to show copy process when I use System.IO.File.Copy function

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

Sep 25 '07 #1
6 5054
You can use ready-made functionality by using the SHFileOperation API
function via P/Invoke

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
---------
"Michael" <mi*******@gmail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.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

Sep 25 '07 #2
"Michael" <mi*******@gmail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.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 _errorDescription 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(ByRef 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 InvalidOperationException("SourceURL 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 InvalidOperationException("Destination 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 InvalidOperationException("Referer 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 ErrorDescription() As String
Get
Return _errorDescription
End Get
End Property
Private Sub ChangeStatus(ByVal NewStatus As DownLoadStatus)
Dim Temp As DownLoadStatus
Temp = _status
_status = NewStatus
RaiseEvent StatusChanged(Me, 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 InvalidOperationException("No Source URL specified")
Exit Sub
End If
If _destPath = "" Then
Throw New InvalidOperationException("No Destination Path
specified")
Exit Sub
End If

Try
Call ChangeStatus(DownLoadStatus.Connecting)
Dim wr As HttpWebRequest = CType(WebRequest.Create(_sourceURL),
HttpWebRequest)
If _referer <"" Then
wr.Referer = _referer
End If
Dim resp As HttpWebResponse = CType(wr.GetResponse(),
HttpWebResponse)
_size = resp.ContentLength
Call ChangeStatus(DownLoadStatus.Connected)
Dim sIn As IO.Stream = resp.GetResponseStream
Dim sOut As New IO.FileStream(_destPath, IO.FileMode.Create)
ReDim bBuffer(BlockSize - 1)
Call ChangeStatus(DownLoadStatus.Downloading)
iRead = sIn.Read(bBuffer, 0, BlockSize)
mRead = iRead
While iRead 0
RaiseEvent ProgressChanged(Me)
sOut.Write(bBuffer, 0, iRead)
iRead = sIn.Read(bBuffer, 0, BlockSize)
mRead += iRead
End While
sIn.Close()
sOut.Close()
Call ChangeStatus(DownLoadStatus.Completed)
Catch ex As Exception
_errorDescription = ex.Message
Call ChangeStatus(DownLoadStatus.ErrorOccured)
End Try
End Sub

End Class
Sep 25 '07 #3
Michael,

Instead of the copy you can use the streamreader and streamwriter while you
than process line by line.

http://msdn2.microsoft.com/en-us/lib...eamwriter.aspx

this is on this page, the benefit of that is that you are only reading and
writting and keep probably your track on the disc
http://msdn2.microsoft.com/en-us/library/36b93480.aspx

Cor

"Michael" <mi*******@gmail.comschreef in bericht
news:11**********************@w3g2000hsg.googlegro ups.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

Sep 25 '07 #4
On Sep 25, 6:49 pm, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
Michael,

Instead of the copy you can use the streamreader and streamwriter while you
than process line by line.

http://msdn2.microsoft.com/en-us/lib...eamwriter.aspx

this is on this page, the benefit of that is that you are only reading and
writting and keep probably your track on the dischttp://msdn2.microsoft.com/en-us/library/36b93480.aspx

Cor

"Michael" <michae...@gmail.comschreef in berichtnews:11**********************@w3g2000hsg.go oglegroups.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- Hide quoted text -

- Show quoted text -
Thank you guys!!
I tried to use SHFileOperation, but I kept receiving error message.
So I write a short piece of code to do this like below.

Dim f1 As New IO.FileStream(source_filepath, IO.FileMode.Open)
Dim len As Double = f1.Length
Dim b(len) As Byte
Dim i As Integer
Dim j As Integer
For i = 0 To len - 1
b(i) = f1.ReadByte
j = i * 100 / len
If j.ToString.Length = 1 Then
System.Console.Write(ControlChars.Back &
ControlChars.Back & j.ToString & "%")
ElseIf j.ToString.Length = 2 Then
System.Console.Write(ControlChars.Back &
ControlChars.Back & ControlChars.Back & j.ToString & "%")
End If
Next

System.Console.WriteLine("Start writing to " & dest_filepath &
"......")
Dim f2 As New IO.FileStream(dest_filepath, IO.FileMode.Create)
For i = 0 To len - 1
f2.WriteByte(b(i))
j = i * 100 / len
If j.ToString.Length = 1 Then
System.Console.Write(ControlChars.Back &
ControlChars.Back & j.ToString & "%")
ElseIf j.ToString.Length = 2 Then
System.Console.Write(ControlChars.Back &
ControlChars.Back & ControlChars.Back & j.ToString & "%")
End If
Next

Sep 25 '07 #5
"Michael" <mi*******@gmail.comschrieb
Thank you guys!!
I tried to use SHFileOperation, but I kept receiving error message.
So I write a short piece of code to do this like below.

Dim f1 As New IO.FileStream(source_filepath,
IO.FileMode.Open)
Dim len As Double = f1.Length
Why double? The type of f1.length is Long.
There are many more errors in the code. First enable Option Strict, then
resolve the errors. After that, post the code if it still does not work.

Option Strict is essential. Otherwise, things that you are not aware of are
done implicitly, and they are sometimes done not they way you want, or
situations that you should handle explicitly are ignored. For example, what
if the file is greater than 2GB? The app would crash! Using Option Strict,
the compiler points you to the narrowing conversion because a 64-Bit integer
does not fit into a 32-Bit integer.

Reading/writing files byte by byte can be very slow. You should read blocks,
for example in 100 KB or 1 MB steps.
Armin

Sep 25 '07 #6
On Sep 25, 7:27 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"Michael" <michae...@gmail.comschrieb
Thank you guys!!
I tried to use SHFileOperation, but I kept receiving error message.
So I write a short piece of code to do this like below.
Dim f1 As New IO.FileStream(source_filepath,
IO.FileMode.Open)
Dim len As Double = f1.Length

Why double? The type of f1.length is Long.

There are many more errors in the code. First enable Option Strict, then
resolve the errors. After that, post the code if it still does not work.

Option Strict is essential. Otherwise, things that you are not aware of are
done implicitly, and they are sometimes done not they way you want, or
situations that you should handle explicitly are ignored. For example, what
if the file is greater than 2GB? The app would crash! Using Option Strict,
the compiler points you to the narrowing conversion because a 64-Bit integer
does not fit into a 32-Bit integer.

Reading/writing files byte by byte can be very slow. You should read blocks,
for example in 100 KB or 1 MB steps.

Armin
Thank you for these kind comments.
These are not my final codes. Anyway, I should have coded more
strictly.
Thank you.

Sep 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tess | last post by:
Hi, Long time reader, first time poster... Any help is appreciated. I have a few questions regarding Winform controls embedded within an html page. For more info please see the appendix. Now,...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: Tom | last post by:
I am having trouble when I read a file and another process is trying to update it. So I need a rountine to copy a file in asp.net. Can anyone assist? Thanks Tom
2
by: Stephen Witter | last post by:
I had previously posted this on the security ng, but haven't had a hit so I was wondering if someone here would be willing to take a stab. I am trying to copy a file to a network drive. I can do...
11
by: F. Michael Miller | last post by:
I'd like to copy the listing of a directory (& sub directories) to a text file in vb.net. I'm looking for teh equivilant of the DOS command dir /s > TargetFile.txt. Thanks!
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
0
by: TwistedPair | last post by:
All, I had some great advice about this a bit ago, but I'm just not good enough with this code to put together all the pieces. The way the code below works is as a service. When it is started,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.