Hallo Leute
Vorweg: Ich bin noch ein Anfänger.
Der folgende Code funktioniert eigentlich ganz gut. Sobald ich allerdings auf die Form (oder woanders hin) klicke, stürzt diese ab. Wie kann ich das verhindern?
Public Sub getfilecounter(ByVal sSrcPath As String, ByVal sDestPath As String, _
Optional ByVal bSubFolder As Boolean = True, Optional ByVal bOverWrite As _
Boolean = True)
If Not System.IO.Directory.Exists(sDestPath) Then
System.IO.Directory.CreateDirectory(sDestPath)
End If
Dim sFiles() As String = System.IO.Directory.GetFiles(sSrcPath)
Dim sFile As String
For i As Integer = 0 To sFiles.Length - 1
sFile = sFiles(i).Substring(sFiles(i).LastIndexOf("\") + 1)
If bOverWrite Or Not System.IO.File.Exists(sDestPath & "\" & sFile) _
Then
d = d + 1
End If
Next i
If bSubFolder Then
Dim sDirs() As String = System.IO.Directory.GetDirectories(sSrcPath)
Dim sDir As String
For i As Integer = 0 To sDirs.Length - 1
If sDirs(i) <> sDestPath Then
sDir = sDirs(i).Substring(sDirs(i).LastIndexOf("\") + 1)
getfilecounter(sDirs(i), sDestPath & "\" & sDir, True, _
bOverWrite)
End If
Next i
End If
End Sub
Public Sub CopyFolder(ByVal sSrcPath As String, ByVal sDestPath As String, _
Optional ByVal bSubFolder As Boolean = True, Optional ByVal bOverWrite As _
Boolean = True)
Try
File.WriteAllText("d:\test\Test.txt", "")
If Not System.IO.Directory.Exists(sDestPath) Then
System.IO.Directory.CreateDirectory(sDestPath)
End If
Dim sFiles() As String = System.IO.Directory.GetFiles(sSrcPath)
Dim sFile As String
For i As Integer = 0 To sFiles.Length - 1
sFile = sFiles(i).Substring(sFiles(i).LastIndexOf("\") + 1)
Label1.Text = sFiles(i)
Label1.Refresh()
If bOverWrite Or Not System.IO.File.Exists(sDestPath & "\" & _
sFile) Then
System.IO.File.Copy(sFiles(i), sDestPath & "\" & sFile, _
True)
ProgressBar1.Value = dprozent2
d2 = d2 + 1
Label2.Text = d2 & " von " & d & " Dateien kopiert"
Label2.Refresh()
dprozent2 = dprozent2 + dprozent
End If
Next i
If bSubFolder Then
Dim sDirs() As String = System.IO.Directory.GetDirectories( _
sSrcPath)
Dim sDir As String
For i As Integer = 0 To sDirs.Length - 1
If sDirs(i) <> sDestPath Then
sDir = sDirs(i).Substring(sDirs(i).LastIndexOf("\") + 1)
CopyFolder(sDirs(i), sDestPath & "\" & sDir, True, _
bOverWrite)
End If
Next i
End If
Catch ex As SystemException
MessageBox.Show("Error: " & ex.Message, My.Application.Info.Title)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button2.Click
getfilecounter("D:\test\t1", "D:\test\t2", True, False)
If d = 0 Then
lable3text = "Keine Syncronisation notwendig"
Else
dprozent = 100 / d
dprozent2 = dprozent
d2 = 0
ProgressBar1.Maximum = dprozent * d + 1
CopyFolder("D:\test\t1", "D:\test\t2", True, False)
lable3text = " Syncronisation abgeschlossen. Es wurden " & d & "" & _
"Dateien kopiert."
ProgressBar1.Value = ProgressBar1.Maximum
End If
Label3.Text = lable3text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
End
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
End Sub
End Class |