Simple improvement: make WinUtil's Shortcut Function Do Less (Lazier) (#1903)

* Add Extra Comments to 'Invoke-WPFShortcut' to make the Logic a Bit Clearer

* Make The 'Invoke-WPFShortcut' Function Lazier for Added Efficiency and to not Display False Info whenever the User Cancels the 'Save As' Operation
This commit is contained in:
Mr.k 2024-05-01 21:42:38 +03:00 committed by GitHub
parent bdf9c8378c
commit f5f506b9c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,6 +17,7 @@ function Invoke-WPFShortcut {
[bool]$RunAsAdmin = $false
)
# Preper the Shortcut Fields and add an a Custom Icon if it's available at "$env:TEMP\cttlogo.png", else don't add a Custom Icon.
$iconPath = $null
Switch ($ShortcutToAdd) {
"WinUtil" {
@ -33,11 +34,18 @@ function Invoke-WPFShortcut {
}
}
# Show a File Dialog Browser, to let the User choose the Name and Location of where to save the Shortcut
$FileBrowser = New-Object System.Windows.Forms.SaveFileDialog
$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$FileBrowser.Filter = "Shortcut Files (*.lnk)|*.lnk"
$FileBrowser.FileName = $DestinationName
$FileBrowser.ShowDialog() | Out-Null
# Do an Early Return if The Save Shortcut operation was cancel by User's Input.
$FileBrowserResult = $FileBrowser.ShowDialog()
$DialogResultEnum = New-Object System.Windows.Forms.DialogResult
if (-not ($FileBrowserResult -eq $DialogResultEnum::OK)) {
return
}
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($FileBrowser.FileName)