• 0

[VB.NET]Copy File with progress bar


Question

I'm attempting to make a program that will copy files reclusively in VB.NET.

basically I have a function that calculates the total size of every file in the directory and sub directories then goes through all of them and calls the CopyFile function.

this works alright in that my progress bar actually updates while the file is being copied instead of using filenames like I had been. It just seams to be going a lot slower. Five seconds for a 156k file as opposed to bellow three.

Is there a better way?

Clint

        Private Function CopyFile(ByVal OldFile As String, ByVal NewFile As String)
            Me.FileProgressbar.Value = 0
            Dim FS As New FileStream(OldFile, FileMode.Open)
            Dim FW As New FileStream(NewFile, FileMode.CreateNew)
            Dim Buffer() As Byte
            'Get the bytes from file to a byte array
            ReDim Buffer(FS.Length - 1)
            Me.FileProgressbar.Maximum = FS.Length
            FS.Read(Buffer, 0, Buffer.Length)
            'Do your stuff :-)
            For i As Int32 = 0 To Buffer.Length - 1
                Me.FileProgressbar.Value += 1
                FW.WriteByte(Buffer(i))
                Me.TotalProgressbar.Value += 1
            Next
            FS.Close()
            FW.Close()
        End Function

Link to comment
https://www.neowin.net/forum/topic/341607-vbnetcopy-file-with-progress-bar/
Share on other sites

11 answers to this question

Recommended Posts

  • 0

I'm already using basic multi threading.

Per another board I changed it to write and update the progressbars every kb and that helped a lot.

It copied a 75.5 Meg file 1.53125 Seconds faster then File.Copy :)

I will try and get this "Proper Threading" thing figured out in the morning after I've had some rest. :p

Clint

  • 0

Yes I was, now it copies each Kb

My Code with a single progressbar took 42.374 seconds to copy a 483,005 kb file, File.Copy took 46.343 seconds

two progressbars:

My Code: 68.28 Seconds

File.Copy: 37.343 Seconds

No Progressbars:

Mine: 23.796 Seconds

.NET: 41.499 Seconds

that last one surprised me.

figured that at best mine would be just as fast as File.Copy, but apparently not...

Clint

  • 0
  GoodOlClint said:
Yes I was, now it copies each Kb

My Code with a single progressbar took 42.374 seconds to copy a 483,005 kb file, File.Copy took 46.343 seconds

two progressbars:

My Code: 68.28 Seconds

File.Copy: 37.343 Seconds

No Progressbars:

Mine: 23.796 Seconds

.NET: 41.499 Seconds

that last one surprised me.

figured that at best mine would be just as fast as File.Copy, but apparently not...

Clint

586183376[/snapback]

You seems calculating the time taken by using codes WHICH slows the copying process, if so then you should remove this codes and see the difference ;)

  • 0
  Elagizy said:
You seems calculating the time taken by using codes WHICH slows the copying process, if so then you should remove this codes and see the difference  ;)

no I used this to get how long it took:

Sub debug()
        Dim copy As New clsCopyFiles(Me.ProgressBar2, Me.ProgressBar1, Me.Label8, Me.Label7)
        Dim start1 As Date = Now
        copy.CopyFile("C:\largefile.wmv", "C:\video1.wmv")
        Dim finish1 As Date = Now
        Dim start2 As Date = Now
        File.Copy("C:\largefile.wmv:\video2.wmv")
        Dim finish2 As Date = Now
        MsgBox(finish1.TimeOfDay.TotalSeconds - start1.TimeOfDay.TotalSeconds & vbNewLine & finish2.TimeOfDay.TotalSeconds - start2.TimeOfDay.TotalSeconds)
    End Sub

so I wasn't calculating untill after the copying was done.

Clint

  • 0
  GoodOlClint said:
no I used this to get how long it took:

Sub debug()
        Dim copy As New clsCopyFiles(Me.ProgressBar2, Me.ProgressBar1, Me.Label8, Me.Label7)
        Dim start1 As Date = Now
        copy.CopyFile("C:\largefile.wmv", "C:\video1.wmv")
        Dim finish1 As Date = Now
        Dim start2 As Date = Now
        File.Copy("C:\largefile.wmv:\video2.wmv")
        Dim finish2 As Date = Now
        MsgBox(finish1.TimeOfDay.TotalSeconds - start1.TimeOfDay.TotalSeconds & vbNewLine & finish2.TimeOfDay.TotalSeconds - start2.TimeOfDay.TotalSeconds)
    End Sub

so I wasn't calculating untill after the copying was done.

Clint

586183620[/snapback]

The problem with your method, though, is that you can only transfer files that can fit in memory. Unless you are now implementing a buffer that outputs every megabyte or so.

  • 0

It's strange that you're like trying to reinvent the wheel, i mean yeah the base class library offers a static file copy method for you to use, why try to do it all over again, and furthermore, i'm sure their implementation of file copy is much more efficient.

  • 0
  Winston said:
It's strange that you're like trying to reinvent the wheel, i mean yeah the base class library offers a static file copy method for you to use, why try to do it all over again, and furthermore, i'm sure their implementation of file copy is much more efficient.

586185592[/snapback]

hehe I'm one of those people who can't stand knowing things work. I have to know why it works.

I learn best by reinventing the wheel. :-p

Besides, I'm in highschool, I can do anything right :shifty: ?

Clint

  • 0
  GoodOlClint said:
hehe I'm one of those people who can't stand knowing things work. I have to know why it works.

I learn best by reinventing the wheel. :-p

Besides, I'm in highschool, I can do anything right :shifty: ?

Clint

586185657[/snapback]

Yeah i guess it's good to understand how things work. But i think that's how you'd do it in general, however, i think the file.copy in which the framework provides, invokes a Win32 API, and i'm pretty sure MS does the file copy a little different, peraps more efficient, since they made the OS after all.

  • 0
  Winston said:
Yeah i guess it's good to understand how things work. But i think that's how you'd do it in general, however, i think the file.copy in which the framework provides, invokes a Win32 API, and i'm pretty sure MS does the file copy a little different, peraps more efficient, since they made the OS after all.

586186072[/snapback]

Oh I have no doubt that it is extremly more efficient. after all, they are a mega corp with billions to spend and I'm a 17 year old with $5 in my pocket...

Clint

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Minor Problems over the years with Windows 11 Pro on my original Intel 10700 Desktop i was using and not many problems on my Newer AMD Ryzen 7 7700X. Overall Windows 11 works extremely well, if don't do too many modifications, and do the regular security updates, and keep 3rd party apps up to date, and users shouldn't have any issues, and if a user does, should be a minor little issue that is easily fixable My goal is to have the 2 remaining household systems replaced for Windows 11 Compatible machines, hopefully by early September if not sooner.
    • Bill Gates says he'll donate 99 percent of his wealth to Africa by Hamid Ganji Microsoft co-founder and tech billionaire Bill Gates has pledged to donate a significant chunk of his personal wealth to African countries. As reported by the BBC, Gates's funding will be spent on improving health and education infrastructure in Africa over the next twenty years. The Gates Foundation has played an active role in improving public health and education in Africa over the past decades. Bill Gates aims to double down on that effort by donating most of his fortune to Africa. Last month, he also said that 99% of his fortune, which could exceed $200 billion, will go to African countries by 2045. Speaking at the African Union (AU) headquarters in Ethiopia's capital, Addis Ababa, Gates said, "By unleashing human potential through health and education, every country in Africa should be on a path to prosperity." The tech billionaire also told young African innovators to start relying on AI to improve health and education in their countries and use the technology to benefit the entire continent. He introduced Rwanda as a successful example of using AI in healthcare to identify high-risk pregnancies. "Africa largely skipped traditional banking and now you have a chance, as you build your next generation healthcare systems, to think about how AI is built into that," Gates added. By donating 99 percent of his personal fortune by 2045, Bill Gates can still maintain his position as one of the richest people on earth. According to Bloomberg, Gates's current net worth is around $175 billion. Gates's decision to donate 99 percent of his wealth to Africa came after the US administration cut USAID funding to African countries following the DOGE investigations. In a recent interview with the New York Times, Bill Gates called out Elon Musk for cutting the USAID budget, saying Musk has a role in the death of the poorest children on earth.
    • Very cool next level tech (for Witcher 4, CybePunk 2, etc.), but I'd be more impressed with seeing this at 4k than through a too low-bitrate YouTube ~1080p filter.
    • I was thinking, and was going to post, almost exactly what @Brandon Hjust posted!
  • Recent Achievements

    • Week One Done
      Adam Todd earned a badge
      Week One Done
    • Contributor
      Ed B went up a rank
      Contributor
    • One Month Later
      moporcho earned a badge
      One Month Later
    • One Month Later
      Parotel earned a badge
      One Month Later
    • Reacting Well
      Cryptecks earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      202
    2. 2
      snowy owl
      146
    3. 3
      ATLien_0
      133
    4. 4
      Xenon
      120
    5. 5
      +FloatingFatMan
      110
  • Tell a friend

    Love Neowin? Tell a friend!