-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBackgroundProgress.vb
239 lines (218 loc) · 11.8 KB
/
BackgroundProgress.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Imports System
Imports System.IO
Imports System.Linq
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Public Class BackgroundProgress
Inherits System.Windows.Forms.Form
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Private Sub InitializeComponent()
Me.lblStatus = New System.Windows.Forms.Label()
Me.imgLoading = New System.Windows.Forms.PictureBox()
Me.pbTaskProgress = New wyDay.Controls.Windows7ProgressBar()
Me.bwFolderOperations = New System.ComponentModel.BackgroundWorker()
CType(Me.imgLoading, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'lblStatus
Me.lblStatus.AutoSize = True
Me.lblStatus.Location = New System.Drawing.Point(34, 14)
Me.lblStatus.Name = "lblStatus"
Me.lblStatus.Size = New System.Drawing.Size(0, 13)
'imgLoading
Me.imgLoading.Image = My.Resources.loading4
Me.imgLoading.Location = New System.Drawing.Point(12, 12)
Me.imgLoading.Name = "imgLoading"
Me.imgLoading.Size = New System.Drawing.Size(16, 16)
Me.imgLoading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
Me.imgLoading.TabStop = False
'pbTaskProgress
Me.pbTaskProgress.ContainerControl = Me
Me.pbTaskProgress.Location = New System.Drawing.Point(12, 34)
Me.pbTaskProgress.Name = "pbTaskProgress"
Me.pbTaskProgress.ShowInTaskbar = True
Me.pbTaskProgress.Size = New System.Drawing.Size(310, 23)
'bwFolderOperations
Me.bwFolderOperations.WorkerReportsProgress = True
Me.bwFolderOperations.WorkerSupportsCancellation = True
'BackgroundProgress
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(334, 71)
Me.Controls.Add(Me.pbTaskProgress)
Me.Controls.Add(Me.imgLoading)
Me.Controls.Add(Me.lblStatus)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Icon = My.Resources.document_properties
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "BackgroundProgress"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
'Me.Location = New System.Drawing.Size((My.Computer.Screen.WorkingArea.Width/2) - 167, (My.Computer.Screen.WorkingArea.Height/2) - 35.5)
CType(Me.imgLoading, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents bwFolderOperations As System.ComponentModel.BackgroundWorker
Private pbTaskProgress As wyDay.Controls.Windows7ProgressBar
Private lblStatus As System.Windows.Forms.Label
Private imgLoading As System.Windows.Forms.PictureBox
Sub New()
InitializeComponent()
Dim theme As WalkmanLib.Theme = Settings.GetTheme()
WalkmanLib.ApplyTheme(theme, Me, True)
If components IsNot Nothing Then WalkmanLib.ApplyTheme(theme, components.Components, True)
End Sub
Dim i As Integer
Dim WasError As Boolean = False
''' delete, deletePath
''' copy, copyFromPath, copyToPath
Sub bwFolderOperations_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bwFolderOperations.DoWork
Dim command As String = DirectCast(e.Argument, String())(0)
Dim sourcePath As String = DirectCast(e.Argument, String())(1)
Dim DirectoryProperties As New DirectoryInfo(sourcePath)
Try
If command = "delete" Then
'Get file list (2%)
'Delete files (95% total)
'Get folder list (1%)
'Delete folders (1%)
'Delete final folder (1%)
Me.Text = "Deleting """ & DirectoryProperties.Name & """..."
SetStatus("Getting file list... (May take a while)", 0)
Dim SubFiles = DirectoryProperties.GetFiles("*", SearchOption.AllDirectories)
i = 0
For Each SubFile As FileInfo In SubFiles
SetStatus("Deleting file """ & SubFile.Name & """...", CType(((i / SubFiles.Length) * 95) + 2, Integer))
Try
SubFile.Delete()
Catch ex As Exception
Select Case Operations.MessageBox("There was an error deleting """ & SubFile.FullName & """!" & Environment.NewLine & Environment.NewLine & ex.Message,
Windows.Forms.MessageBoxButtons.AbortRetryIgnore, Windows.Forms.MessageBoxIcon.Exclamation, "Error!")
Case Windows.Forms.DialogResult.Abort
Exit For
Case Windows.Forms.DialogResult.Retry
Try
SubFile.Delete()
Catch ex2 As Exception
Select Case Operations.MessageBox("Error retrying delete for """ & SubFile.FullName & """:" & ex.Message,
Windows.Forms.MessageBoxButtons.AbortRetryIgnore, Windows.Forms.MessageBoxIcon.Exclamation, "Error!")
Case Windows.Forms.DialogResult.Abort
Exit For
Case Windows.Forms.DialogResult.Retry
Operations.MessageBox("Already retried!", icon:=Windows.Forms.MessageBoxIcon.Exclamation)
Case Else
End Select
End Try
End Select
End Try
i += 1
Next
SetStatus("Getting folder list...", 97)
Dim SubFolders = DirectoryProperties.GetDirectories("*", SearchOption.AllDirectories)
i = 0
For Each SubFolder As DirectoryInfo In SubFolders.Reverse() ' Reverse to delete deepest directories first
SetStatus("Deleting folder """ & SubFolder.Name & """...", CType(((i / SubFolders.Length) * 1) + 98, Integer))
Try
SubFolder.Delete()
Catch
WasError = True
End Try
i += 1
Next
If WasError Then
For Each SubFolder As DirectoryInfo In SubFolders.Reverse()
Try
SubFolder.Delete()
Catch
End Try
Next
End If
SetStatus("Deleting folder """ & DirectoryProperties.Name & """...", 99)
Threading.Thread.Sleep(100)
DirectoryProperties.Delete()
Me.Close()
Me.Dispose()
ElseIf command = "copy" Then
'Create root dir (1%)
'Get folder list (1%)
'Create folders (1%)
'Get file list (2%)
'Copy files (95%)
Dim targetPath As String = DirectCast(e.Argument, String())(2)
Me.Text = "Copying """ & DirectoryProperties.Name & """ to """ & targetPath & """..."
Directory.CreateDirectory(targetPath)
SetStatus("Getting folder list...", 1)
Dim SubFolders = DirectoryProperties.GetDirectories("*", SearchOption.AllDirectories)
i = 0
For Each SubFolder As DirectoryInfo In SubFolders
SetStatus("Creating folder """ & SubFolder.Name & """...", CType(((i / SubFolders.Length) * 1) + 1, Integer))
Try
Directory.CreateDirectory(targetPath & SubFolder.FullName.Substring(DirectoryProperties.FullName.Length))
Catch
WasError = True
End Try
i += 1
Next
If WasError Then
For Each SubFolder As DirectoryInfo In SubFolders
Try
Directory.CreateDirectory(targetPath & SubFolder.FullName.Substring(DirectoryProperties.FullName.Length))
Catch
End Try
Next
End If
SetStatus("Getting file list... (May take a while)", 3)
Dim SubFiles = DirectoryProperties.GetFiles("*", SearchOption.AllDirectories)
i = 0
For Each SubFile As FileInfo In SubFiles
SetStatus("Copying file """ & SubFile.Name & """...", CType(((i / SubFiles.Length) * 95) + 5, Integer))
Try
SubFile.CopyTo(targetPath & SubFile.FullName.Substring(DirectoryProperties.FullName.Length))
Catch ex As Exception
Select Case Operations.MessageBox("There was an error copying """ & SubFile.FullName & """ to """ & targetPath &
SubFile.FullName.Substring(DirectoryProperties.FullName.Length) & """!" &
Environment.NewLine & Environment.NewLine & ex.Message,
Windows.Forms.MessageBoxButtons.AbortRetryIgnore, Windows.Forms.MessageBoxIcon.Exclamation, "Error!")
Case Windows.Forms.DialogResult.Abort
Exit For
Case Windows.Forms.DialogResult.Retry
Try
SubFile.CopyTo(targetPath & SubFile.FullName.Substring(DirectoryProperties.FullName.Length))
Catch ex2 As Exception
Select Case Operations.MessageBox("Error retrying copy for """ & SubFile.FullName & """:" & ex.Message,
Windows.Forms.MessageBoxButtons.AbortRetryIgnore, Windows.Forms.MessageBoxIcon.Exclamation, "Error!")
Case Windows.Forms.DialogResult.Abort
Exit For
Case Windows.Forms.DialogResult.Retry
Operations.MessageBox("Already retried!", icon:=Windows.Forms.MessageBoxIcon.Exclamation)
End Select
End Try
End Select
End Try
i += 1
Next
Threading.Thread.Sleep(100)
Me.Close()
Me.Dispose()
End If
Catch ex As Exception
If Not Settings.Loaded Then Settings.Init()
WalkmanLib.ErrorDialog(ex, Settings.GetTheme())
Me.Close()
End Try
End Sub
''' <summary>Sets the status label text to statusText, and the progress bar percent to statusPercent.</summary>
''' <param name="statusText">The text to display on the status label</param>
''' <param name="statusPercent">The percent to set the progress bar value to. 0-100</param>
Sub SetStatus(statusText As String, statusPercent As Integer)
lblStatus.Text = statusText
pbTaskProgress.Value = statusPercent
End Sub
End Class