I am using Visual Studio 2010 and coding in Visual Basic.
I have to display a progress bar while copying a directory. I have never worked with a progress bar before and not sure where to start. Here is the code I currently have.
If My.Computer.FileSystem.DirectoryExists(filePath & "IETMS\" & installFile) Then
frmWait.Show()
My.Computer.FileSystem.CopyDirectory(strFileName, filePath & "IETMS", True)
ListView1.Items.Clear()
testReload()
frmWait.Close()
Else
My.Computer.FileSystem.CreateDirectory(filePath & "IETMS\" & installFile)
frmWait.Show()
My.Computer.FileSystem.CopyDirectory(strFileName, filePath & "IETMS", True)
ListView1.Items.Clear()
testReload()
frmWait.Close()
End If
I am assuming that I need to calculate the size of the source folder and then monitor the destination folder size and set the progress bar max to the source folder size and set the value of the progress bar to the destination size, but I am not sure how to go about doing this.
You can count the files in the source directory and then every so often count the files in the destination directory. To count the files in all subdirectories you can use a recursive sub:
Private Sub CountFiles(InFolder As String, ByRef Result As Integer)
Result += IO.Directory.GetFiles(InFolder).Count
For Each f As String In IO.Directory.GetDirectories(InFolder)
CountFiles(f, Result)
Next
End Sub
To use this do
Dim FileCount as Integer = 0
CountFiles("C:\test", FileCount)
Messagebox.Show(FileCount.ToString)
Set the progressbar to the percentage value like pbProgress.Value = CInt(DestCount/SourceCount * 100)
.
Edit: Following up on your question: You should use for example a backgroundworker, or a task, or a thread, to perform the copy and then update the progressbar in a Timer. For example you can create a sub that does the copying and then start the sub in a new task:
Private WithEvents tmrUpdatePBG As Timer
Private Sub StartCopy(SourceFolder As String, DestFolder As String)
'copy copy copy
CopyComplete()
End Sub
Private Sub CopyComplete()
tmrUpdatePBG.Stop()
End Sub
[...]
'Whereever you start the copy process
Dim ct As New Task(Sub() StartCopy("C:\source", "C:\dest"))
ct.Start()
tmrUpdatePBG = New Timer
tmrUpdatePBG.Interval = 1000
tmrUpdatePBG.Start()
tmrUpdatePGB
would be the timer. In the tick event update the progressbar. It is started when the copying process starts and stops when the process is complete.