如何获取快捷方式图标文件的路径?

问题描述 投票:0回答:2

有没有办法获得快捷方式的.ico路径? 我知道如何更改快捷方式图标,但是如何找到快捷方式图标文件的路径?

powershell icons shortcut
2个回答
4
投票

您可以使用以下功能。
它可以处理“常规”快捷方式文件 (.lnk) 以及 Internet 快捷方式文件 (.url)

function Get-ShortcutIcon {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias('FullName')]
        [string]$Path  # needs to be an absulute path
    )
    switch ([System.IO.Path]::GetExtension($Path)) {
        '.lnk'  {
            $WshShell = New-Object -ComObject WScript.Shell
            $shortcut = $WshShell.CreateShortcut($Path)
            $iconPath = $shortcut.IconLocation
            $iconInfo = if ($iconPath -match '^,(\d+)') {
                [PsCustomObject]@{ IconPath = $shortcut.TargetPath; IconIndex = [int]$matches[1] }
            }
            else {
                [PsCustomObject]@{ IconPath = $iconPath; IconIndex = 0 }
            }

            # clean up
            $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut)
            $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WshShell)
            [System.GC]::Collect()
            [System.GC]::WaitForPendingFinalizers()

            # return the icon information
            $iconInfo
        }
        '.url' {
            $content = Get-Content -Path $Path -Raw
            $iconPath  = [regex]::Match($content, '(?im)^\s*IconFile\s*=\s*(.*)').Groups[1].Value
            $iconIndex = [regex]::Match($content, '(?im)^\s*IconIndex\s*=\s*(\d+)').Groups[1].Value
            [PsCustomObject]@{ IconPath = $iconPath; IconIndex = [int]$iconIndex }
        }
        default { Write-Warning "'$Path' does not point to a '.lnk' or '.url' shortcut file.." }
    }
}

0
投票

出于某种原因,我无法让 Theo 的代码运行。为了清楚起见和提供更多信息,我对其进行了修改。但是,它仅适用于链接。此外,我们并没有真正解决OP问题来实际提取并获取嵌入的图标文件。

将以下内容放入您的 $PROFILE 中(或作为外部

*.ps1
函数加载。)。

function Get-ShortcutIcon {
    $private:Path = $args[0]

    $ws = New-Object -ComObject WScript.Shell
    $sc = $ws.CreateShortcut($Path)

    $scInd = 0                              # Default to 0
    $scLoc = $sc.IconLocation               # C:\Windows\System32\shell32.dll,164
    $scTar = $sc.TargetPath                 # "C:\Windows\System32\control.exe"
    $scArg = $sc.Arguments                  # "ncpa.cpl"

    if ($scLoc -match '^(.+),(\d+)') {
        # C:\Windows\System32\shell32.dll,164
        $scLoc  = [string]$matches[1]       # C:\Windows\System32\shell32.dll
        $scInd  = [int]$matches[2]          # 164
    }

    $Tar    = "  {0,-20} : " -f "Shortcut Target"           # $scTar
    $Arg    = "  {0,-20} : " -f "Shortcut Arguments"        # $scArg
    $Loc    = "  {0,-20} : " -f "Icon Location"             # $scLoc
    $Ind    = "  {0,-20} : " -f "Icon Location Index"       # $scInd

    Write-Host -f Yellow "`nFile Icon info:`n"
    Write-Host -f DarkGray "$Tar" -Non;     Write-Host -f White "$scTar"    # 
    Write-Host -f DarkGray "$Arg" -Non;     Write-Host -f Gray "$scArg" # 
    Write-Host -f DarkGray "$Loc" -Non;     Write-Host -f DarkYellow "$scLoc"   # 
    Write-Host -f DarkGray "$Ind" -Non;     Write-Host -f DarkYellow "$scInd"   # 
    
    # Clean up
    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sc)
    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ws)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
    Write-Host
}

Set-Alias -Name GetIcon -Value Get-ShortcutIcon -Description 'lnk icon' -Scope Local -Option ReadOnly, Private

快捷方式的输出如下所示:

Icon Location Index

Drawback / Issues

  • 不能用于
    *.exe
    文件
  • 实际上没有找到嵌入的图标文件。
© www.soinside.com 2019 - 2024. All rights reserved.