Copy File progress...

G

Guest

Usig VB .NET 2003, I'm writing a simple app that copies 1 or more files.
Requiremtns state that I need two progress bars one for current file copy
progress and one for overall progress.

My problem is that I'm not sure how to obtain progress about a file as it is
being copied. I've read some thread about using the SHFileOperation API but
I'm lost as to how to implemt that in .NET.

Can someone give me a pointer to some doco? Also an example of the
implemtation and usage of the API wouild be appreciated
 
J

Jay Taplin

This is not a solution... but... I screwed around for about two hours with
this, and got almost there. Unfortunately, I don't have any more time
tonight. Create this code as a class. The only problem with it is that I
can't pass the AddressOf the the UpdateProgress routine to the API function.
I am really not up to speed yet on .NET - I'm sure it can be done, I just
don't know how. I'll try to look at it this weekend.

Public Declare Function CopyFileEx Lib "kernel32" Alias "CopyFileExA"
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal
lpProgressRoutine As Long, ByVal lpData As Long, ByVal pbCancel As Long,
ByVal dwCopyFlags As Long) As Long

Private Const COPY_FILE_RESTARTABLE As Long = &H2
Private Const PROGRESS_CONTINUE As Long = 0
Private Const PROGRESS_CANCEL As Long = 1
Private Const PROGRESS_STOP As Long = 2
Private Const PROGRESS_QUIET As Long = 3

Public Event Progress(ByVal TotalFileSize As Object, ByVal
TotalBytesTransferred As Object, ByVal Cancel As Boolean)

Public Sub CopyFile(ByVal SourceFile As String, ByVal DestFile As
String)
CopyFileEx(SourceFile, DestFile, AddressOf UpdateProgress, 0, False,
COPY_FILE_RESTARTABLE)
End Sub

Private Function UpdateProgress(ByVal TotalFileSize As Object, ByVal
TotalBytesTransferred As Object, ByVal StreamSize As Object, ByVal
StreamBytesTransferred As Object, ByVal dwStreamNumber As Long, ByVal
dwCallbackReason As Long, ByVal hSourceFile As Integer, ByVal
hDestinationFile As Integer, ByVal lpData As Integer) As Integer
Dim blnCancel As Boolean

RaiseEvent Progress(TotalFileSize, TotalBytesTransferred, blnCancel)

If blnCancel = True Then
Return PROGRESS_CANCEL
Else
Return PROGRESS_CONTINUE
End If
End Function
 
M

Morten Wennevik

Hi,

Using stream objects and Stream.Read/Write, you will be able to know how much has been written to the new file. Update the progress bar from within the write procedure. I haven't used it for copying files, but it works well when downloading files on the internet.
 
G

Guest

Can you give a sample of the code?

Morten Wennevik said:
Hi,

Using stream objects and Stream.Read/Write, you will be able to know how much has been written to the new file. Update the progress bar from within the write procedure. I haven't used it for copying files, but it works well when downloading files on the internet.
 
M

Morten Wennevik

Something like this might do. Feed it any stream and it will return a byte[] of all the bytes in the stream. CurLength is a property that will be updatet approximately every 8096 bytes read with the the number of bytes read so far.

private byte[] ReadStream(Stream s)
{
try
{
byte[] buffer = new byte[8096];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
// read keeps track of how many bytes were actually read
// especially with network streams, this may vary
int read = s.Read(buffer, 0, buffer.Length);

// no more to read? return what we have read
if (read <= 0)
return ms.ToArray();

ms.Write(buffer, 0, read);

CurLength = ms.Length;
}
}
}
catch(Exception ex)
{
HandleException(ex);
return null;
}

}

Can you give a sample of the code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top