totally true, I have a progress bar.
this is the code I have:
Private Sub Form_Initialize()<br />
InitCommonControls()<br />
End Sub<br />
Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32<br />
<br />
Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32<br />
Private _totalFileSize As Long = 0<br />
Private _totalBytesCopied As Long = 0<br />
Private _copyProgressRoutine As CopyProgressRoutine<br />
Private Source, Source2, Source3, Source4 As String<br />
Dim UserProfileName = SystemInformation.UserName<br />
<br />
<br />
Private Destination As String = "H:\Documents and Settings\" & UserProfileName<br />
<br />
Dim frmProgBar As New frmProgBar<br />
<br />
<br />
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click<br />
<br />
Me.Close()<br />
Dim fbdBrowse As New FolderBrowserDialog<br />
If fbdBrowse.ShowDialog() = DialogResult.OK Then<br />
<br />
Source = fbdBrowse.SelectedPath<br />
Source2 = Source & "\" & "My Documents"<br />
'Source3 = fbdBrowse.SelectedPath & "\Desktop"<br />
'Source4 = fbdBrowse.SelectedPath & "\Favorites"<br />
Else : End<br />
<br />
End If<br />
<br />
If (MessageBox.Show("You have chosen to copy all files and folders from: " & Chr(13) & Source & Chr(13) & "to: " & Chr(13) & Destination, "Data Mover", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = DialogResult.OK) Then<br />
<br />
<br />
frmProgBar.Show()<br />
<br />
GetTotalFileSize(New System.IO.DirectoryInfo(Source2))<br />
_copyProgressRoutine = New CopyProgressRoutine(AddressOf CopyProgress)<br />
CopyFiles(New System.IO.DirectoryInfo(Source), Destination)<br />
MessageBox.Show("Data move done, make sure that the user doesn't have custom folders on d:\ or c:\", "Data Mover", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)<br />
<br />
Else<br />
MessageBox.Show("Action Canceled", "Data Mover", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)<br />
<br />
End If<br />
<br />
<br />
End<br />
<br />
End Sub<br />
<br />
Public Function CopyDirectory(ByVal Src As String, ByVal Dest As String, Optional _<br />
ByVal bQuiet As Boolean = False) As Boolean<br />
If Not Directory.Exists(Src) Then<br />
Throw New DirectoryNotFoundException("The directory " & Src & " does not exists")<br />
End If<br />
If Directory.Exists(Dest) AndAlso Not bQuiet Then<br />
If MessageBox.Show("directory " & Dest & " already exists." & vbCrLf & _<br />
"If you continue, any files with the same name will be overwritten", _<br />
"Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, _<br />
MessageBoxDefaultButton.Button2) = DialogResult.Cancel Then Exit Function<br />
End If<br />
<br />
'add Directory Seperator Character (\) for the string concatenation shown later<br />
'If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then<br />
' Dest += Path.DirectorySeparatorChar<br />
' End If<br />
If Not Directory.Exists(Dest) Then Directory.CreateDirectory(Dest)<br />
Dim Files As String()<br />
Files = Directory.GetFileSystemEntries(Src)<br />
Dim element As String<br />
For Each element In Files<br />
If Directory.Exists(element) Then<br />
'if the current FileSystemEntry is a directory,<br />
'call this function recursively<br />
CopyDirectory(element, Dest & Path.GetFileName(element), True)<br />
Else<br />
'the current FileSystemEntry is a file so just copy it<br />
File.Copy(element, Dest & Path.GetFileName(element), True)<br />
End If<br />
Next<br />
Return True<br />
End Function<br />
Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32<br />
frmProgBar.ProgressBar1.Value = Convert.ToInt32((_totalBytesCopied + totalBytesTransferred) / _totalFileSize * 100)<br />
Application.DoEvents()<br />
End Function<br />
Private Sub GetTotalFileSize(ByVal folder As System.IO.DirectoryInfo)<br />
For Each fi As System.IO.FileInfo In folder.GetFiles<br />
_totalFileSize += fi.Length<br />
Next<br />
For Each di As System.IO.DirectoryInfo In folder.GetDirectories<br />
GetTotalFileSize(di)<br />
Next<br />
End Sub<br />
Private Sub CopyFiles(ByVal folder As System.IO.DirectoryInfo, ByVal destinationFolder As String)<br />
If Not System.IO.Directory.Exists(destinationFolder) Then<br />
System.IO.Directory.CreateDirectory(destinationFolder)<br />
End If<br />
For Each fi As System.IO.FileInfo In folder.GetFiles<br />
CopyFileEx(fi.FullName, destinationFolder & "\" & fi.Name, _copyProgressRoutine, 0, 0, 0)<br />
_totalBytesCopied += fi.Length<br />
Next<br />
For Each di As System.IO.DirectoryInfo In folder.GetDirectories<br />
CopyFiles(di, di.FullName.Replace(Source, Destination))<br />
Next<br />
End Sub<br />
<br />
<br />
End Class
it's the value which is integer I guess and should be converted to something else.
thanks for the quick response btw.
Do you know what I'm doing wrong?
Greetzzz,
CornElvis
www.jackied.nl
-- modified at 9:25 Friday 16th June, 2006
|