- 1 year ago
How do I programmatically get the icons for WIN10 / MS Store Apps?
To programmatically retrieve the icons for Windows 10/Microsoft Store Apps, you can use the Windows API functions and PowerShell. Here's an example using PowerShell:
powershell# Import necessary Windows API functions
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, out ushort lpiIcon);
[DllImport("user32.dll", SetLastError = true)]
public static extern int DestroyIcon(IntPtr hIcon);
}
"@
# Function to retrieve the icon for an app
function GetAppIcon([string]$appPath) {
$iconPath = New-Object System.Text.StringBuilder 260
$iconIndex = 0
$hIcon = [Win32]::ExtractAssociatedIcon([IntPtr]::Zero, $iconPath, [ref]$iconIndex)
if ($hIcon -ne [IntPtr]::Zero) {
# Save the icon as an .ico file
$iconFile = "$env:TEMP\AppIcon.ico"
[System.Drawing.Icon]::FromHandle($hIcon).ToBitmap().Save($iconFile, [System.Drawing.Imaging.ImageFormat]::Icon)
# Destroy the icon handle
[Win32]::DestroyIcon($hIcon)
return $iconFile
}
return $null
}
# Example usage
$appPath = "C:\Path\To\Your\App.exe"
$iconFile = GetAppIcon $appPath
if ($iconFile) {
Write-Host "Icon file: $iconFile"
} else {
Write-Host "Failed to retrieve app icon."
}
In this example, the GetAppIcon
function takes the path to the application executable file (.exe
) as input. It uses the ExtractAssociatedIcon
function from the shell32.dll
library to retrieve the associated icon handle for the application. It then saves the icon as an .ico
file in a temporary location and returns the path of the saved icon file.
You can modify the $appPath
variable to specify the path to the application executable file for which you want to retrieve the icon. After running the script, it will output the path of the saved icon file ($iconFile
) if the retrieval is successful.
Note that this example retrieves the associated icon for a given executable file. If the application uses a different method to store its icons (e.g., as separate resource files), additional techniques may be required to retrieve them.