-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOperations.vb
479 lines (437 loc) · 26.9 KB
/
Operations.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms
Public Class Operations
Enum TimeChangeEnum
Creation = 1 ' have to give them values,
LastAccess = 2 ' so the "Case ... And ..."
LastWrite = 3 ' below works.
End Enum
Shared Sub SetSelectDateDialogValue(path As String, useUTC As Boolean, type As TimeChangeEnum)
Try
Select Case DirectCast(type, Integer)
Case TimeChangeEnum.Creation And CType(useUTC, Integer)
SelectDateDialog.dateTimePicker.Value = File.GetCreationTimeUtc(path)
Case TimeChangeEnum.Creation
SelectDateDialog.dateTimePicker.Value = File.GetCreationTime(path)
Case TimeChangeEnum.LastAccess And CType(useUTC, Integer)
SelectDateDialog.dateTimePicker.Value = File.GetLastAccessTimeUtc(path)
Case TimeChangeEnum.LastAccess
SelectDateDialog.dateTimePicker.Value = File.GetLastAccessTime(path)
Case TimeChangeEnum.LastWrite And CType(useUTC, Integer)
SelectDateDialog.dateTimePicker.Value = File.GetLastWriteTimeUtc(path)
Case TimeChangeEnum.LastWrite
SelectDateDialog.dateTimePicker.Value = File.GetLastWriteTime(path)
End Select
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub SetTime(path As String, useUTC As Boolean, type As TimeChangeEnum, time As Date)
Try
Select Case DirectCast(type, Integer)
Case TimeChangeEnum.Creation And CType(useUTC, Integer)
File.SetCreationTimeUtc (path, time)
Case TimeChangeEnum.Creation
File.SetCreationTime (path, time)
Case TimeChangeEnum.LastAccess And CType(useUTC, Integer)
File.SetLastAccessTimeUtc(path, time)
Case TimeChangeEnum.LastAccess
File.SetLastAccessTime (path, time)
Case TimeChangeEnum.LastWrite And CType(useUTC, Integer)
File.SetLastWriteTimeUtc (path, time)
Case TimeChangeEnum.LastWrite
File.SetLastWriteTime (path, time)
End Select
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
' cMBb = CustomMsgBoxBtn
Public Const cMBbRelaunch As String = "Relaunch as Admin"
Public Const cMBbRunSysTool As String = "Run System Tool as Admin"
Public Const cMBbCancel As String = "Cancel"
Public Const cMBTitle As String = "Access denied!"
Private Shared Function Win32FromHResult(HResult As Integer) As Integer
'getting Win32 error from HResult:
' https://docs.microsoft.com/en-us/dotnet/standard/io/handling-io-errors#handling-ioexception
' https://devblogs.microsoft.com/oldnewthing/20061103-07/?p=29133
' https://stackoverflow.com/a/426467/2999220
Return (HResult And &H0000FFFF)
End Function
'32 (0x20) = ERROR_SHARING_VIOLATION: The process cannot access the file because it is being used by another process.
Private Const shareViolation As Integer = &H20
Shared Sub Rename(sourcePath As String, targetName As String)
Dim fileProperties As New FileInfo(sourcePath)
Dim fullTargetName = fileProperties.DirectoryName & Path.DirectorySeparatorChar & targetName
Try
If WalkmanLib.IsFileOrDirectory(fullTargetName).HasFlag(PathEnum.Exists) AndAlso sourcePath <> fullTargetName Then
Select Case MessageBox("Target """ & fullTargetName & """ already exists! Remove first?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Case DialogResult.Yes
Delete(fullTargetName)
Case DialogResult.Cancel
Exit Sub
End Select
End If
fileProperties.MoveTo(fullTargetName)
PropertiesDotNet.LoadNew(fileProperties.FullName)
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If WalkmanLib.RunAsAdmin("cmd", "/c ren """ & sourcePath & """ """ & targetName & """ && pause") AndAlso
MessageBox("Read new location?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(fullTargetName)
End If
End Select
Catch ex As IOException When Win32FromHResult(ex.HResult) = shareViolation
If MessageBox("File """ & sourcePath & """ is in use! Open Handle Manager?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
HandleManager(sourcePath)
End If
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub Move(sourcePath As String, targetPath As String, useShell As Boolean)
Try
If useShell Then
Dim pathInfo = WalkmanLib.IsFileOrDirectory(sourcePath)
If pathInfo.HasFlag(PathEnum.IsFile) Then
My.Computer.FileSystem.MoveFile(sourcePath, targetPath, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)
ElseIf pathInfo.HasFlag(PathEnum.IsDirectory) Then
My.Computer.FileSystem.MoveDirectory(sourcePath, targetPath, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)
End If
Else
If WalkmanLib.IsFileOrDirectory(targetPath).HasFlag(PathEnum.Exists) AndAlso sourcePath <> targetPath Then
Select Case MessageBox("Target """ & targetPath & """ already exists! Remove first?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Case DialogResult.Yes
Delete(targetPath)
Case DialogResult.Cancel
Exit Sub
End Select
End If
File.Move(sourcePath, targetPath)
End If
PropertiesDotNet.LoadNew(targetPath)
Catch ex As OperationCanceledException ' ignore user cancellation
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If WalkmanLib.RunAsAdmin("cmd", "/c move """ & sourcePath & """ """ & targetPath & """ & pause") AndAlso
MessageBox("Read new location?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Select
Catch ex As IOException When Win32FromHResult(ex.HResult) = shareViolation
If MessageBox("File """ & sourcePath & """ is in use! Open Handle Manager?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
HandleManager(sourcePath)
End If
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub Copy(sourcePath As String, targetPath As String, useShell As Boolean)
Try
Dim pathInfo = WalkmanLib.IsFileOrDirectory(sourcePath)
If useShell Then
If pathInfo.HasFlag(PathEnum.IsFile) Then
My.Computer.FileSystem.CopyFile(sourcePath, targetPath, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)
ElseIf pathInfo.HasFlag(PathEnum.IsDirectory) Then
My.Computer.FileSystem.CopyDirectory(sourcePath, targetPath, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)
End If
Else
If pathInfo.HasFlag(PathEnum.IsFile) Then
If WalkmanLib.IsFileOrDirectory(targetPath).HasFlag(PathEnum.Exists) AndAlso sourcePath <> targetPath AndAlso
MessageBox("Target """ & targetPath & """ already exists! Are you sure you want to overwrite it?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.No Then
Exit Sub
End If
Dim sourceStream As FileStream = Nothing
Dim targetStream As FileStream = Nothing
Try
sourceStream = File.OpenRead(sourcePath)
targetStream = File.Open(targetPath, FileMode.Create, FileAccess.Write)
WalkmanLib.StreamCopy(sourceStream, targetStream, "Copying """ & sourcePath & """ to """ & targetPath & """...",
"File Copy", Sub(s, e)
If e.Error IsNot Nothing Then
PropertiesDotNet.ErrorParser(e.Error)
ElseIf MessageBox("Read new location?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Sub)
Catch
If sourceStream IsNot Nothing Then sourceStream.Dispose()
If targetStream IsNot Nothing Then targetStream.Dispose()
Throw
End Try
ElseIf pathInfo.HasFlag(PathEnum.IsDirectory) Then
BackgroundProgress.bwFolderOperations.RunWorkerAsync({"copy", sourcePath, targetPath})
BackgroundProgress.ShowDialog()
End If
End If
' if we do file StreamCopy then don't show message
If Not (useShell = False AndAlso pathInfo.HasFlag(PathEnum.IsFile)) AndAlso
MessageBox("Read new location?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
Catch ex As OperationCanceledException ' ignore user cancellation
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If WalkmanLib.RunAsAdmin("xcopy", "/F /H /K """ & sourcePath & """ """ & targetPath & "*""") AndAlso
MessageBox("Read new location?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Select
Catch ex As IOException When Win32FromHResult(ex.HResult) = shareViolation
If MessageBox("A file is in use! Open Handle Manager on """ & targetPath & """?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
HandleManager(targetPath)
Else
PropertiesDotNet.ErrorParser(ex)
End If
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub Delete(path As String, Optional useShell As Boolean = False, Optional recycleOption As Microsoft.VisualBasic.FileIO.RecycleOption =
Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently)
Try
Dim pathInfo = WalkmanLib.IsFileOrDirectory(path)
If useShell Then
If pathInfo.HasFlag(PathEnum.IsFile) Then
My.Computer.FileSystem.DeleteFile(path, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, recycleOption)
ElseIf pathInfo.HasFlag(PathEnum.IsDirectory) Then
My.Computer.FileSystem.DeleteDirectory(path, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, recycleOption)
End If
Else
Dim pathAttrs As FileAttributes = File.GetAttributes(path)
If pathInfo.HasFlag(PathEnum.IsFile) Then
File.Delete(path)
ElseIf pathAttrs.HasFlag(FileAttributes.ReparsePoint) Then
Directory.Delete(path)
ElseIf pathInfo.HasFlag(PathEnum.IsDirectory) Then
BackgroundProgress.bwFolderOperations.RunWorkerAsync({"delete", path})
BackgroundProgress.ShowDialog()
End If
End If
Catch ex As OperationCanceledException ' ignore user cancellation
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If WalkmanLib.RunAsAdmin("cmd", "/c del """ & path & """ & pause") Then
Threading.Thread.Sleep(500)
End If
End Select
Catch ex As IOException When Win32FromHResult(ex.HResult) = shareViolation
If MessageBox("File """ & path & """ is in use! Open Handle Manager?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
HandleManager(path)
End If
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub CreateShortcut(sourcePath As String, targetPath As String)
Try
If WalkmanLib.IsFileOrDirectory(targetPath).HasFlag(PathEnum.Exists) AndAlso sourcePath <> targetPath AndAlso
MessageBox("Target """ & targetPath & """ already exists! Are you sure you want to overwrite the shortcut's Target Path?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.No Then
Exit Sub
End If
Dim newShortcutPath As String = WalkmanLib.CreateShortcut(targetPath, sourcePath)
If MessageBox("Show properties for created Shortcut?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(newShortcutPath)
End If
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
Dim scriptPath As String = Environment.GetEnvironmentVariable("temp") & Path.DirectorySeparatorChar & "createShortcut.vbs"
Using writer As New StreamWriter(File.Open(scriptPath, FileMode.Create))
writer.WriteLine("Set lnk = WScript.CreateObject(""WScript.Shell"").CreateShortcut(""" & targetPath & """)")
writer.WriteLine("lnk.TargetPath = """ & sourcePath & """")
writer.WriteLine("lnk.Save")
End Using
If WalkmanLib.RunAsAdmin("wscript", scriptPath) AndAlso
MessageBox("Show properties for created Shortcut?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Select
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub CreateSymlink(sourcePath As String, targetPath As String)
Try
If WalkmanLib.IsFileOrDirectory(targetPath).HasFlag(PathEnum.Exists) AndAlso sourcePath <> targetPath Then
Select Case MessageBox("Target """ & targetPath & """ already exists! Remove first?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Case DialogResult.Yes
Delete(targetPath)
Case DialogResult.Cancel
Exit Sub
End Select
End If
Dim pathInfo = WalkmanLib.IsFileOrDirectory(sourcePath)
WalkmanLib.CreateSymLink(targetPath, sourcePath, pathInfo.HasFlag(PathEnum.IsDirectory))
If MessageBox("Show properties for created Symlink?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
Dim pathInfo = WalkmanLib.IsFileOrDirectory(sourcePath)
Dim arguments As String = "/c mklink " & If(pathInfo.HasFlag(PathEnum.IsFile), """", "/d """) & targetPath & """ """ & sourcePath & """ & pause"
If WalkmanLib.RunAsAdmin("cmd", arguments) AndAlso
MessageBox("Show properties for created Symlink?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Select
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub CreateHardlink(sourcePath As String, targetPath As String)
Try
If WalkmanLib.IsFileOrDirectory(targetPath).HasFlag(PathEnum.Exists) AndAlso sourcePath <> targetPath Then
Select Case MessageBox("Target """ & targetPath & """ already exists! Remove first?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Case DialogResult.Yes
Delete(targetPath)
Case DialogResult.Cancel
Exit Sub
End Select
End If
WalkmanLib.CreateHardLink(targetPath, sourcePath)
If MessageBox("Show properties for created Hardlink?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If WalkmanLib.RunAsAdmin("cmd", "/c mklink /h """ & targetPath & """ """ & sourcePath & """ & pause") AndAlso
MessageBox("Show properties for created Hardlink?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Select
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Sub CreateJunction(sourcePath As String, targetPath As String)
Try
If WalkmanLib.IsFileOrDirectory(targetPath).HasFlag(PathEnum.Exists) AndAlso sourcePath <> targetPath Then
Select Case MessageBox("Target """ & targetPath & """ already exists! Remove first?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Case DialogResult.Yes
Delete(targetPath)
Case DialogResult.Cancel
Exit Sub
End Select
End If
WalkmanLib.CreateJunction(targetPath, sourcePath, True)
If MessageBox("Show properties for created Junction?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
Catch ex As UnauthorizedAccessException When Not WalkmanLib.IsAdmin()
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If WalkmanLib.RunAsAdmin("cmd", "/c mklink /j """ & targetPath & """ """ & sourcePath & """ & pause") AndAlso
MessageBox("Show properties for created Junction?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
PropertiesDotNet.LoadNew(targetPath)
End If
End Select
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
End Sub
Shared Function SetAttribute(path As String, attribute As FileAttributes, addOrRemove As Boolean) As Boolean
Try
Dim fileAttributes As FileAttributes
fileAttributes = File.GetAttributes(path)
If addOrRemove Then
fileAttributes = fileAttributes Or attribute
Else ' Or (C# |) adds an attribute, And Not (C# & ~) removes an attribute
fileAttributes = fileAttributes And Not attribute
End If
File.SetAttributes(path, fileAttributes)
Return True
Catch ex As UnauthorizedAccessException When _
Not WalkmanLib.IsAdmin() AndAlso
Not attribute.HasFlag(FileAttributes.Compressed) AndAlso
Not attribute.HasFlag(FileAttributes.Encrypted) AndAlso
Not attribute.HasFlag(FileAttributes.Temporary) AndAlso
Not attribute.HasFlag(FileAttributes.ReparsePoint) AndAlso
Not attribute.HasFlag(FileAttributes.SparseFile)
Select Case WalkmanLib.CustomMsgBox(ex.Message, Settings.GetTheme(), cMBTitle, cMBbRelaunch, cMBbRunSysTool, cMBbCancel, MessageBoxIcon.Exclamation, ownerForm:=PropertiesDotNet)
Case cMBbRelaunch
PropertiesDotNet.RestartAsAdmin()
Case cMBbRunSysTool
If SetAttributeAsAdmin(path, attribute, addOrRemove) Then
Threading.Thread.Sleep(100)
End If
End Select
Catch ex As IOException When Win32FromHResult(ex.HResult) = shareViolation
If MessageBox("File """ & path & """ is in use! Open Handle Manager?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
HandleManager(path)
End If
Catch ex As Exception
PropertiesDotNet.ErrorParser(ex)
End Try
Return False
End Function
Private Shared Function SetAttributeAsAdmin(path As String, attribute As FileAttributes, addOrRemove As Boolean) As Boolean
Select Case attribute
Case FileAttributes.ReadOnly
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "r """ & path & """ /l")
Case FileAttributes.Hidden
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "h """ & path & """ /l")
Case FileAttributes.System
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "s """ & path & """ /l")
Case FileAttributes.Archive
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "a """ & path & """ /l")
Case FileAttributes.NotContentIndexed
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "i """ & path & """ /l")
Case FileAttributes.Offline
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "o """ & path & """ /l")
Case FileAttributes.NoScrubData
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "x """ & path & """ /l")
Case FileAttributes.IntegrityStream
Return WalkmanLib.RunAsAdmin("attrib", If(addOrRemove, "+", "-") & "v """ & path & """ /l")
Case Else
Throw New InvalidOperationException("Invalid Attribute specified: " & attribute.ToString())
End Select
End Function
Public Shared Sub HandleManager(filePath As String)
Dim walkmanUtilsPath As String = WalkmanLib.GetWalkmanUtilsPath()
Dim handleManagerPath As String = Path.Combine(walkmanUtilsPath, "HandleManager.exe")
If Not File.Exists(handleManagerPath) Then
MessageBox("Could not find HandleManager in WalkmanUtils install!" & Environment.NewLine & Environment.NewLine &
"Looking for: " & handleManagerPath, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, "Launching HandleManager")
Exit Sub
End If
Process.Start(handleManagerPath, """" & filePath & """")
End Sub
Shared Function MessageBox(text As String, Optional buttons As MessageBoxButtons = 0,
Optional icon As MessageBoxIcon = 0, Optional title As String = Nothing) As DialogResult
If title Is Nothing Then
title = Application.ProductName
End If
' if running on a separate thread then settings isn't loaded. doesn't matter, we can just load, as all settings are immediately saved
If Not Settings.Loaded Then Settings.Init()
Return WalkmanLib.CustomMsgBox(text, Settings.GetTheme(), title, buttons, icon, WinVersionStyle.Win10, PropertiesDotNet)
End Function
Shared Function GetInput(ByRef input As String, Optional windowTitle As String = Nothing, Optional header As String = Nothing, Optional content As String = Nothing) As DialogResult
If Not Settings.Loaded Then Settings.Init()
Return WalkmanLib.InputDialog(input, Settings.GetTheme(), header, windowTitle, content, ownerForm:=PropertiesDotNet)
End Function
End Class