473,386 Members | 1,720 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,386 software developers and data experts.

Copy File progress...

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
Jan 13 '06 #1
4 3344
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

"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:59**********************************@microsof t.com...
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

Jan 13 '06 #2
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.

On Fri, 13 Jan 2006 21:47:01 +0100, hz****@nopost.com"" <hz*************@discussions.microsoft.com> wrote:
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


--
Happy coding!
Morten Wennevik [C# MVP]
Jan 14 '06 #3
Can you give a sample of the code?

"Morten Wennevik" wrote:
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.

On Fri, 13 Jan 2006 21:47:01 +0100, hz****@nopost.com"" <hz*************@discussions.microsoft.com> wrote:
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


--
Happy coding!
Morten Wennevik [C# MVP]

Jan 18 '06 #4
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;
}

}

On Wed, 18 Jan 2006 17:16:08 +0100, hz****@nopost.com"" <hz*************@discussions.microsoft.com> wrote:
Can you give a sample of the code?

"Morten Wennevik" wrote:
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.

On Fri, 13 Jan 2006 21:47:01 +0100, hz****@nopost.com"" <hz*************@discussions.microsoft.com> wrote:
> 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
>


--
Happy coding!
Morten Wennevik [C# MVP]


--
Happy coding!
Morten Wennevik [C# MVP]
Jan 21 '06 #5

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

Similar topics

0
by: GK1402 | last post by:
I am writing a web app that will copy large files (250-3,000 MB) and wanted to know the easiest way to implement some sort of progress bar? Each copy will only have one file, but I would like a...
2
by: Peter Rilling | last post by:
Suppose that I want to get periodic status info when coping a file, how do I do this? For instance, if my program copies a large files, how can I be notified of the progress so that I can display...
16
by: Adam Witney | last post by:
Hi, Is it possible for the COPY command to read data from a file, but skip specific columns? Thanks Adam
3
by: Reidar | last post by:
I want to copy a file from a server to a local machine. The process should be shown displayed by a progressbar. Has anybody an example of this? reidarT
9
by: Alan T | last post by:
Is it possible to copy a file from one location to another? eg. from C:\Temp\Document\TestDoc.doc to C:\Deploy\Document\TestDoc.doc
0
by: Skywalker | last post by:
Hi. Can you please help me? I have problem;-) I am copying from one computer to another 50 MB large text file. For now is everything working. My question is, if I can in VBA for MS ACCESS show to...
1
by: Talal Itani | last post by:
Hello, I need the program I am writing to duplicate a folder, from one drive to another drive. I need a progress report, and write-verify. What is a good way to do that? I am using C#...
6
by: Michael | last post by:
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...
6
by: Johnny Jörgensen | last post by:
Using File.Copy in the System.IO namespace, it's easy to copy one or more files. But I wonder: Is it possible to get windows to display its File Copy Progress dialog at the same time - or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.