Skip to content

Commit 23f9e52

Browse files
committed
Closing the program now triggers a forced kill of compact.exe if it's active - with a warning.
Added option to restart as administrator if you encounter an UnauthorizedAccessException
1 parent 6925dab commit 23f9e52

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

WindowsApp1/Compact.vb

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Imports System.Runtime.InteropServices
33
Imports System.Text
44
Imports System.Text.RegularExpressions
55
Imports Ookii.Dialogs 'Uses Ookii Dialogs for the non-archaic filebrowser dialog. http://www.ookii.org/Software/Dialogs
6+
Imports System.Management
67

78
Public Class Compact
89
Dim version = "1.3.6"
@@ -274,7 +275,7 @@ Public Class Compact
274275

275276

276277
Dim DIwDString = New DirectoryInfo(wDString)
277-
278+
directorysizeexceptionCount = 0
278279
workingDir = Chr(34) + wDString.ToString() + Chr(34)
279280
chosenDirDisplay.Text = DIwDString.Parent.ToString + " ❯ " + DIwDString.Name.ToString
280281
uncompressedfoldersize = Math.Round(DirectorySize(DIwDString, True), 0)
@@ -362,6 +363,7 @@ Public Class Compact
362363
MyProcess.BeginErrorReadLine()
363364
MyProcess.BeginOutputReadLine()
364365

366+
365367
Try
366368

367369
MyProcess.StandardInput.WriteLine("cd /d " + workingDir)
@@ -466,21 +468,17 @@ Public Class Compact
466468

467469
Else
468470
Try
469-
MyProcess.Kill()
471+
LetsKillStuff()
470472
Catch ex As Exception
471473
End Try
472-
473474
End If
474475
Else
475-
476476
Try
477-
MyProcess.Kill()
477+
LetsKillStuff()
478478
Catch ex As Exception
479479
End Try
480-
481480
End If
482481

483-
484482
End Sub
485483

486484

@@ -608,8 +606,6 @@ Public Class Compact
608606
Dim hasqueryfinished = 0
609607

610608

611-
612-
613609
Private Sub RunCompact(desiredfunction As String)
614610

615611
If desiredfunction = "compact" Then
@@ -668,7 +664,6 @@ Public Class Compact
668664

669665
End If
670666

671-
672667
End Sub
673668

674669

@@ -706,7 +701,9 @@ Public Class Compact
706701
Private Function DirectorySize _
707702
(ByVal dInfo As IO.DirectoryInfo, ByVal includeSubdirectories As Boolean) As Long
708703

704+
709705
Try
706+
710707
Dim totalSize As Long = dInfo.EnumerateFiles().Sum(Function(file) file.Length)
711708

712709
If includeSubdirectories Then
@@ -715,18 +712,31 @@ Public Class Compact
715712

716713
Return totalSize
717714

718-
directorysizeexceptionCount = 0
719-
720715
Catch generatedexceptionname As UnauthorizedAccessException
716+
directorysizeexceptionCount += 1
721717

722-
If directorysizeexceptionCount = 0 Then
723-
MsgBox("This directory contains a subfolder that you do not have permission to access. Please try running the program again as an Administrator." _
724-
& vbCrLf & vbCrLf & "If the problem persists, the subfolder is most likely protected by the System, and by design this program will refuse to let you proceed.")
718+
719+
If directorysizeexceptionCount = 1 Then
725720

726721
overrideCompressFolderButton = 1
727-
directorysizeexceptionCount = 1
728-
End If
722+
directorysizeexceptionCount += 1
723+
724+
If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) = False Then
725+
If MessageBox.Show("This directory contains a subfolder that you do not have permission to access. Please try running the program again as an Administrator." _
726+
& vbCrLf & vbCrLf & "If the problem persists, the subfolder is most likely protected by the System, and by design this program will refuse to let you proceed." _
727+
& vbCrLf & vbCrLf & " Would you like to restart the program as an Administrator?", "Permission Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
728+
729+
RCMenu.RunAsAdmin()
730+
Me.Close()
729731

732+
End If
733+
734+
Else
735+
MsgBox("This directory contains a subfolder that you do not have permission To access." _
736+
& vbCrLf & vbCrLf & "The subfolder is most likely protected by the System, and by design this program will refuse to let you proceed.")
737+
End If
738+
739+
End If
730740

731741
Catch ex As Exception
732742

@@ -737,6 +747,24 @@ Public Class Compact
737747

738748

739749

750+
Private WithEvents KillProc As Process
751+
Private Sub LetsKillStuff()
752+
753+
KillProc = New Process
754+
755+
With KillProc.StartInfo
756+
.FileName = "powershell.exe"
757+
.Arguments = "taskkill /f /im compact.exe"
758+
.UseShellExecute = False
759+
.CreateNoWindow = True
760+
End With
761+
762+
KillProc.Start()
763+
764+
End Sub
765+
766+
767+
740768

741769
'////////////////////TESTING////////////////////
742770

@@ -796,5 +824,4 @@ Public Class Compact
796824
End Sub
797825

798826

799-
800827
End Class

WindowsApp1/CompactGUI.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Reference Include="System.Data" />
6363
<Reference Include="System.Deployment" />
6464
<Reference Include="System.Drawing" />
65+
<Reference Include="System.Management" />
6566
<Reference Include="System.Windows.Forms" />
6667
<Reference Include="System.Xml" />
6768
<Reference Include="System.Core" />

0 commit comments

Comments
 (0)