Hi,
I am trying to make Backup program, that copy files from one location to another one, but I don't know how to allow user to cancel this operation.
The code that I use is: -
Public Class Form1
-
-
Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
-
-
Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32
-
-
Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
-
ProgressBar1.Value = Convert.ToInt32(totalBytesTransferred / totalFileSize * 100)
-
End Function
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim cpr As New CopyProgressRoutine(AddressOf CopyProgress)
-
CopyFileEx("C:\myFile.dll", "D:\myFile.dll", cpr, 0, 0, 0)
-
End Sub
-
-
End Class
-
Thank you
3 2246
you could use a filestream and a buffer instead, then copy it byte by byte. but this is slower then for example io.file.copy.... but you would have more control over the file, you could keep track of the progress, the speed that its being copied at and you could cancel or pause it.
Thank you raids51,
Can you give me an example about that, because I am very fresh in programming.
Thank you
Hi,
I think I found the solution -
Public Class Form1
-
Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
-
Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32
-
-
Private Const COPY_FILE_RESTARTABLE As Long = &H2
-
Private Const PROGRESS_CONTINUE As Long = 0
-
Private Const PROGRESS_CANCEL As Long = 1
-
Dim blnCancel As Boolean = False
-
Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
-
ProgressBar1.Value = Convert.ToInt32(totalBytesTransferred / totalFileSize * 100)
-
Me.Text = ProgressBar1.Value
-
Application.DoEvents()
-
If blnCancel = True Then
-
Return PROGRESS_CANCEL
-
Else
-
Return PROGRESS_CONTINUE
-
End If
-
End Function
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
Dim cpr As New CopyProgressRoutine(AddressOf CopyProgress)
-
CopyFileEx("C:\myFile.dll", "D:\myFile.dll", cpr, 0, blnCancel, 0)
-
End Sub
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
blnCancel = True
-
End Sub
-
End Class
-
Thank you
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tony George |
last post by:
Hi,
I'm having a problem using the Copy module on a ClearCase view that's been
set as "readonly" (i.e. ct chview -readonly <view tag>). I want to copy a
file out of this view and put it in...
|
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,...
|
by: SeanR |
last post by:
I have a function to copare two files. It will first copy the original file
form a different server to a local temp path and then compare that version to
a version that has been restored form tape....
|
by: Pino |
last post by:
What is the fastest way to copy a file from a device to a different
destination?
I know there is a difference between File.Copy and the FileCopy API.
Thanks,
Pino.
|
by: POnfri |
last post by:
Hi,
I have a problem in a peace of code were i'm doing a file copy using
File.Copy. The Source is local and the target is a remote machine.
Example: File.Copy(C:\temp\hi.txt,...
|
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...
|
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...
|
by: Dan |
last post by:
I have an application that I want to use for copying files. My goal is to
copy a files, if a file is in use or not accessible because of security
reasons I want to make note of that file then...
|
by: =?Utf-8?B?UmFkZW5rb19aZWM=?= |
last post by:
I am using standard File.Copy(source,dest,true) method in C# and I have
problem with copying large number of files.
Here is my code:
foreach (FileInfo file in files)
{...
|
by: =?Utf-8?B?R2FuZXNoYQ==?= |
last post by:
Hi I would like to use File.Copy with UNC paths. I have seen this problem
reported by others as well. Can i use File.Copy with UNC paths. ie., i would
like to copy a file from one of the folder to...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
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...
| |