I think I'm just overlooking something simple here, but it's freaking me out.
I'm kind of newby, but totally anymore, but still making stupid mistakes(like anyone I guess....)
here is the thing.
I try to copy a folder from one place to another and everything goes fine, untill I want to create a msgBox with an outcome of Ok or Cancel.
It doesn't matter what I do, I tried different approaches, but it ends it in both times even when I hit ok it stops.
I build in a simple message : stopped just to figure out what it was doing.
here comes the code:
the part in red is the thing I can't get working.
The file copy on it's own works fine, but not with the OKCancel around it.
Thanks already guys and Girls,
Greetzzz,
CornElvis
I'm kind of newby, but totally anymore, but still making stupid mistakes(like anyone I guess....)
here is the thing.
I try to copy a folder from one place to another and everything goes fine, untill I want to create a msgBox with an outcome of Ok or Cancel.
It doesn't matter what I do, I tried different approaches, but it ends it in both times even when I hit ok it stops.
I build in a simple message : stopped just to figure out what it was doing.
here comes the code:
VB.NET:
Private Declare Function InitCommonControls Lib "Comctl32.dll" () As Long
Private Sub Form_Initialize()
InitCommonControls()
End Sub
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
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
Private _totalFileSize As Long = 0
Private _totalBytesCopied As Long = 0
Private _copyProgressRoutine As CopyProgressRoutine
Private Source As String
Dim UserProfileName = SystemInformation.UserName
Private Destination As String = "D:\Documents and Settings\" & UserProfileName & "\My Documents\Program Data\test"
Dim frmProgBar As New frmProgBar
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Me.Hide()
Dim fbdBrowse As New FolderBrowserDialog
If fbdBrowse.ShowDialog() = DialogResult.OK Then
Source = fbdBrowse.SelectedPath
Else : End
End If
[COLOR=red]Dim answ As Microsoft.VisualBasic.MsgBoxResult = MsgBoxResult.Cancel
MsgBox("Following folder structure will be created: " & Chr(13) & Destination & Chr(13) & Chr(13) & "and the following data will be copied from " & Source, MsgBoxStyle.OKCancel, "Confirm")
If answ <> MsgBoxResult.OK Then
MsgBox("stopped", MsgBoxStyle.OKOnly, )[/COLOR]
[COLOR=red] End[/COLOR]
[COLOR=red] Else[/COLOR]
[COLOR=red] frmProgBar.Show()[/COLOR]
[COLOR=red] GetTotalFileSize(New System.IO.DirectoryInfo(Source))
_copyProgressRoutine = New CopyProgressRoutine(AddressOf CopyProgress)
CopyFiles(New System.IO.DirectoryInfo(Source), Destination)[/COLOR]
[COLOR=red] End If[/COLOR]
End Sub
Public Function CopyDirectory(ByVal Src As String, ByVal Dest As String, Optional _
ByVal bQuiet As Boolean = False) As Boolean
If Not Directory.Exists(Src) Then
Throw New DirectoryNotFoundException("The directory " & Src & " does not exists")
End If
If Directory.Exists(Dest) AndAlso Not bQuiet Then
If MessageBox.Show("directory " & Dest & " already exists." & vbCrLf & _
"If you continue, any files with the same name will be overwritten", _
"Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = DialogResult.Cancel Then Exit Function
End If
'add Directory Seperator Character (\) for the string concatenation shown later
'If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then
' Dest += Path.DirectorySeparatorChar
' End If
If Not Directory.Exists(Dest) Then Directory.CreateDirectory(Dest)
Dim Files As String()
Files = Directory.GetFileSystemEntries(Src)
Dim element As String
For Each element In Files
If Directory.Exists(element) Then
'if the current FileSystemEntry is a directory,
'call this function recursively
CopyDirectory(element, Dest & Path.GetFileName(element), True)
Else
'the current FileSystemEntry is a file so just copy it
File.Copy(element, Dest & Path.GetFileName(element), True)
End If
Next
Return True
End Function
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
frmProgBar.ProgressBar1.Value = Convert.ToInt32((_totalBytesCopied + totalBytesTransferred) / _totalFileSize * 100)
Application.DoEvents()
End Function
Private Sub GetTotalFileSize(ByVal folder As System.IO.DirectoryInfo)
For Each fi As System.IO.FileInfo In folder.GetFiles
_totalFileSize += fi.Length
Next
For Each di As System.IO.DirectoryInfo In folder.GetDirectories
GetTotalFileSize(di)
Next
End Sub
Private Sub CopyFiles(ByVal folder As System.IO.DirectoryInfo, ByVal destinationFolder As String)
If Not System.IO.Directory.Exists(destinationFolder) Then
System.IO.Directory.CreateDirectory(destinationFolder)
End If
For Each fi As System.IO.FileInfo In folder.GetFiles
CopyFileEx(fi.FullName, destinationFolder & "\" & fi.Name, _copyProgressRoutine, 0, 0, 0)
_totalBytesCopied += fi.Length
Next
For Each di As System.IO.DirectoryInfo In folder.GetDirectories
CopyFiles(di, di.FullName.Replace(Source, Destination))
Next
End Sub
End Class
the part in red is the thing I can't get working.
The file copy on it's own works fine, but not with the OKCancel around it.
Thanks already guys and Girls,
Greetzzz,
CornElvis