473,766 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5082
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).
EZNamespaceExte nsions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensio ns.Net : Develop all shell extensions,expl orer bars and BHOs
rapidly in .Net
---------
"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

Sep 25 '07 #2
"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
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*******@gmai l.comschreef in bericht
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

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...@gmai l.comschreef in berichtnews:11* *************** ******@w3g2000h sg.googlegroups .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(s ource_filepath, IO.FileMode.Ope n)
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.Leng th = 1 Then
System.Console. Write(ControlCh ars.Back &
ControlChars.Ba ck & j.ToString & "%")
ElseIf j.ToString.Leng th = 2 Then
System.Console. Write(ControlCh ars.Back &
ControlChars.Ba ck & ControlChars.Ba ck & j.ToString & "%")
End If
Next

System.Console. WriteLine("Star t writing to " & dest_filepath &
"......")
Dim f2 As New IO.FileStream(d est_filepath, IO.FileMode.Cre ate)
For i = 0 To len - 1
f2.WriteByte(b( i))
j = i * 100 / len
If j.ToString.Leng th = 1 Then
System.Console. Write(ControlCh ars.Back &
ControlChars.Ba ck & j.ToString & "%")
ElseIf j.ToString.Leng th = 2 Then
System.Console. Write(ControlCh ars.Back &
ControlChars.Ba ck & ControlChars.Ba ck & j.ToString & "%")
End If
Next

Sep 25 '07 #5
"Michael" <mi*******@gmai l.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(s ource_filepath,
IO.FileMode.Ope n)
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...@free net.dewrote:
"Michael" <michae...@gmai l.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(s ource_filepath,
IO.FileMode.Ope n)
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
2597
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, for the questions. 1. A button on my control executes the System.IO.Directory.GetDirectories funtion (the scanned directory resides on the hosting web server). What credentials is this
18
4893
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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
6
3277
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
3773
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 it on the web server but not from a client. Here is the code: Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity currentWindowsIdentity...
11
2145
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
2935
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 working with cifs (shares). there is another solution to copy files than file.copy in .net?
7
11639
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 is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
13
11153
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 writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
0
1204
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, it watches a folder that is set in a registry key, and if there is a file created in that folder, it copies it to a destination folder, also defined in a registry key. This works fine, for individual files, and small files, but as mentioned...
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.