• 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

    • 2TB Samsung 990 EVO Plus SSD drops to a great price again by Fiza Ali The 2TB Samsung 990 EVO Plus SSD is currently available on Amazon, Samsung, and Newegg at a great price, only $0.44 higher than its historic low. So, if you have been wanting to upgrade your storage solution, you may want to check it out (purchase links down below). The SSD features NAND memory, delivering sequential read speeds of up to 7,250MB/s and write speeds of up to 6,300MB/s. For random read (QD32), the 2TB version reaches up to 1,000K IOPS, and for random write (QD32), it achieves up to 1,350K IOPS. Furthermore, it carries a Total Bytes Written (TBW) rating of 1,200. The 990 EVO Plus is equipped with Intelligent TurboWrite 2.0 for enhanced performance with large files. Moreover, the drive features a nickel-coated controller that helps manage heat, reduce power usage, and prevent overheating. The SSD supports both PCIe 4.0 x4 and PCIe 5.0 x2, promising faster data transfer speeds and better performance. Additionally, with Samsung's Magician Software, the SSD keeps its firmware up to date, adds extra security, and allows for continuous drive health monitoring. Finally, the Samsung 990 EVO Plus SSD comes with a five year limited warranty, offering long-term reliability and peace of mind. 2TB Samsung 990 EVO Plus SSD: $129.99 (Amazon US) - $129.99 (Samsung US) - $129.99 (Newegg) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. You can also check out other SSD deals here. For hard disk drives, you can head over to our HDD deals section to see if anything from there matches your requirements. Make sure you also browse through Amazon US, Amazon UK, and Newegg US to find some other great tech deals. As an Amazon Associate, we earn from qualifying purchases.
    • As visually pleasing as elementaryos (and pantheon) is, I found it to be a little too far behind on some things. I wish they would package their desktop environment for use on other distros. Unfortunately their dev team is pretty small and it just naturally takes a long time for them to get things polished.
    • I was thinking, and was going to post, almost exactly what @Brandon Hjust posted!   WARNING: possible spoilers I also love these videos she makes:   
    • The Witcher 4 tech demo shows off the RPG in action on a base PS5 at 60FPS by Pulasthi Ariyasinghe Epic Games' The State of Unreal 2025 keynote happened today, giving a look at its latest improvements to the Unreal Engine, and other developments and partnerships that the Fortnite maker is involved in. The show kicked things off with a bang, giving fans a live on-stage presentation of The Witcher 4 alongside developer CD Projekt RED. Seen below, the tech demo of the hugely anticipated RPG features the new trilogy's protagonist, Ciri, as she explores Kovir, the new mainland that CD Projekt is letting players explore. Following a monster trail, Ciri and her horse Kelpie go through mountains and forests before reaching the town of Valdrest. Surprisingly, the demo is said to be running on a base PlayStation 5, and it even manages to stay at 60 frames per second despite the stunning visuals. The studio said that the technologies it is using will benefit all platforms that it is targeting for the RPG. The Witcher 4 is coming to PC, Xbox Series X|S, and PlayStation 5 consoles, though a release date has not been revealed yet. "We started our partnership with Epic Games to push open-world game technology forward. To show this early look at the work we’ve been doing using Unreal Engine running at 60 FPS on PlayStation 5, is a significant milestone — and a testament of the great cooperation between our teams," said joint CEO of CD Projekt RED Michał Nowakowski. "But we're far from finished. I look forward to seeing more advancements and inspiring technology from this partnership as development of The Witcher 4 on Unreal Engine 5 continues." The demo showcases tech like Unreal Animation Framework, Nanite Foliage rendering, MetaHuman technology with Mass AI crowd scaling, and more from Epic Games' latest engine developments. Unreal Engine 5.6 is said to include many of these improvements, letting any developer use the tools on their games for better visuals and optimizations.
    • The same guy who promised AGI in a year, and later he said: yes, but no really.
  • 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
      145
    3. 3
      ATLien_0
      133
    4. 4
      Xenon
      120
    5. 5
      +FloatingFatMan
      108
  • Tell a friend

    Love Neowin? Tell a friend!