Click here to Skip to main content
15,569,222 members
Home / Discussions / C#
   

C#

 
QuestionProgress Bar makes copying painfully slow, a little help please? Pin
kiasta27-Aug-14 18:26
kiasta27-Aug-14 18:26 
AnswerRe: Progress Bar makes copying painfully slow, a little help please? Pin
Ravi Bhavnani27-Aug-14 18:44
professionalRavi Bhavnani27-Aug-14 18:44 
GeneralRe: Progress Bar makes copying painfully slow, a little help please? Pin
kiasta28-Aug-14 5:20
kiasta28-Aug-14 5:20 
AnswerRe: Progress Bar makes copying painfully slow, a little help please? Pin
Roger50027-Aug-14 19:04
Roger50027-Aug-14 19:04 
GeneralRe: Progress Bar makes copying painfully slow, a little help please? Pin
kiasta28-Aug-14 6:03
kiasta28-Aug-14 6:03 
GeneralRe: Progress Bar makes copying painfully slow, a little help please? Pin
Roger50028-Aug-14 12:51
Roger50028-Aug-14 12:51 
GeneralRe: Progress Bar makes copying painfully slow, a little help please? Pin
kiasta29-Aug-14 9:46
kiasta29-Aug-14 9:46 
AnswerRe: Progress Bar makes copying painfully slow, a little help please? Pin
OriginalGriff27-Aug-14 23:36
mvaOriginalGriff27-Aug-14 23:36 
To add to what the others say, you are also doing this byte by byte!
Buffer it: allocate a buffer of say 1/2 meg and fill it. Write it, update progress. Fill it, write it, update progress.

But I do it in a background worker like this:
C#
/// <summary>
/// Do the actual file move.
/// </summary>
/// <param name="src"></param>
/// <param name="dst"></param>
/// <param name="worker"></param>
/// <param name="prMain"></param>
private void MoveFile(string src, string dst, BackgroundWorker worker = null, ProgressReport prMain = null)
    {
    if (src != dst)
        {
        // Copy the file itself.
        int iSrc = src.IndexOf(':');
        int iDst = dst.IndexOf(':');
        FileInfo fiSrc = new FileInfo(src);
        if (fiSrc.Length < blockSize || (iSrc > 0 && iDst > 0 && iSrc == iDst && src.Substring(0, iSrc) == dst.Substring(0, iDst)))
            {
            // On same drive or trivial size - move it via the file system
            if (doCopyOnly)
                {
                File.Copy(src, dst);
                }
            else
                {
                File.Move(src, dst);
                }
            }
        else
            {
            // Needs to be moved "properly".
            using (Stream sr = new FileStream(src, FileMode.Open))
                {
                using (Stream sw = new FileStream(dst, FileMode.Create))
                    {
                    long total = sr.Length;
                    long bytes = 0;
                    long cnt = total;
                    int progress = 0;
                    while (cnt > 0)
                        {
                        int n = sr.Read(transfer, 0, blockSize);
                        sw.Write(transfer, 0, n);
                        bytes += n;
                        cnt -= n;
                        int percent = (int)((bytes * 100) / total);
                        if (progress != percent)
                            {
                            // Report progress
                            progress = percent;
                            if (worker != null && prMain != null)
                                {
                                ProgressReport pr = new ProgressReport
                                {
                                    FilesCount = prMain.FilesCount,
                                    FileNumber = prMain.FileNumber,
                                    Filename = prMain.Filename,
                                    Action = prMain.Action,
                                    FilePercentage = percent
                                };
                                worker.ReportProgress(-prMain.FileNumber, pr);
                                }
                            }
                        }
                    }
                }
            // Update the fileinfo.
            FileInfo fiDst = new FileInfo(dst);
            fiDst.Attributes = fiSrc.Attributes;
            fiDst.CreationTime = fiSrc.CreationTime;
            fiDst.CreationTimeUtc = fiSrc.CreationTimeUtc;
            fiDst.IsReadOnly = fiSrc.IsReadOnly;
            fiDst.LastAccessTime = fiSrc.LastAccessTime;
            fiDst.LastAccessTimeUtc = fiSrc.LastAccessTimeUtc;
            fiDst.LastWriteTime = fiSrc.LastWriteTime;
            fiDst.LastWriteTimeUtc = fiSrc.LastWriteTimeUtc;
            // And remove the source
            if (!doCopyOnly)
                {
                File.Delete(src);
                }
            }
        }
    }

That's a general purpose method which does a lot more than you probably want to, but it moves a couple of gig files pretty quickly and reports progress.
You looking for sympathy?
You'll find it in the dictionary, between sympathomimetic and sympatric
(Page 1788, if it helps)

GeneralRe: Progress Bar makes copying painfully slow, a little help please? Pin
kiasta28-Aug-14 6:08
kiasta28-Aug-14 6:08 
QuestionFacing Problems in your Code Pin
Member 1019429727-Aug-14 17:07
Member 1019429727-Aug-14 17:07 
AnswerRe: Facing Problems in your Code Pin
Dave Kreskowiak27-Aug-14 18:27
mveDave Kreskowiak27-Aug-14 18:27 
QuestionRe: Facing Problems in your Code Pin
ZurdoDev28-Aug-14 5:23
professionalZurdoDev28-Aug-14 5:23 
AnswerRe: Facing Problems in your Code Pin
Gerry Schmitz28-Aug-14 6:36
mveGerry Schmitz28-Aug-14 6:36 
QuestionC# 2013 Pin
shawnjots27-Aug-14 14:12
shawnjots27-Aug-14 14:12 
AnswerRe: C# 2013 Pin
Richard MacCutchan27-Aug-14 23:17
mveRichard MacCutchan27-Aug-14 23:17 
AnswerRe: C# 2013 Pin
Sibeesh KV29-Sep-14 20:26
professionalSibeesh KV29-Sep-14 20:26 
Questionget string from via Regex Pin
jojoba2027-Aug-14 8:02
jojoba2027-Aug-14 8:02 
AnswerRe: get string from via Regex Pin
Rob Philpott27-Aug-14 10:13
Rob Philpott27-Aug-14 10:13 
QuestionRe: get string from via Regex Pin
jojoba2027-Aug-14 10:43
jojoba2027-Aug-14 10:43 
AnswerRe: get string from via Regex Pin
Bernhard Hiller27-Aug-14 21:40
Bernhard Hiller27-Aug-14 21:40 
AnswerRe: get string from via Regex Pin
Ravi Bhavnani27-Aug-14 10:36
professionalRavi Bhavnani27-Aug-14 10:36 
QuestionHow to write Context menu helper class Pin
Tejas Vaishnav26-Aug-14 22:09
professionalTejas Vaishnav26-Aug-14 22:09 
AnswerRe: How to write Context menu helper class Pin
Eddy Vluggen26-Aug-14 23:27
professionalEddy Vluggen26-Aug-14 23:27 
GeneralRe: How to write Context menu helper class Pin
Rob Philpott26-Aug-14 23:38
Rob Philpott26-Aug-14 23:38 
GeneralRe: How to write Context menu helper class Pin
Tejas Vaishnav27-Aug-14 0:00
professionalTejas Vaishnav27-Aug-14 0:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.