我正在寻找一个Powershell脚本来提取可执行文件的.ico。
我发现一些看起来像这样的工具:
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
$folder = "C:\TMP_7423\icons"
md $folder -ea 0 | Out-Null
dir $env:windir *.exe -ea 0 -rec |
ForEach-Object {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($_.FullName)
Write-Progress "Extracting Icon" $baseName
[System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName).ToBitmap().Save("$folder\$BaseName.ico")
}
但是问题是,我想对另一个工具使用powershell到exe转换器,该工具具有直接在生成的.exe中放置图标的功能。我想将提取的ico用于生成的可执行文件。
问题是ps1到exe转换器不能与ico提取器生成的任何图标一起使用。
但是,问题是当我使用在互联网上找到的.ico时,ps1到exe转换器可以工作。
那么,您是否有任何可执行的.ico提取器,它们不会提取损坏的ico而是我们可以在互联网上找到的普通.ico?
先感谢您 ! :)
最佳答案
可能ps1到exe转换器需要正确的.ICO,并且您的代码使用ToBitmap().Save()
,这不会以ico格式保存图像信息。为此,您需要使用提取的图标本身的Save()
方法。
不幸的是,此Save方法的参数只能是一个流对象,而不仅仅是路径。
尝试:
Add-Type -AssemblyName System.Drawing
$folder = "C:\TMP_7423\icons"
if (!(Test-Path -Path $folder -PathType Container)) {
$null = New-Item -Path $folder -ItemType Directory
}
Get-ChildItem -Path $env:windir -Filter '*.exe' -Recurse |
ForEach-Object {
Write-Progress "Extracting Icon $($_.BaseName)"
$ico = [System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName)
$target = Join-Path -Path $folder -ChildPath ('{0}.ico' -f $_.BaseName)
# create a filestream object
$stream = [System.IO.FileStream]::new($target, [IO.FileMode]::Create, [IO.FileAccess]::Write)
# save as ICO format to the filestream
$ico.Save($stream)
# remove the stream object
$stream.Dispose()
$ico.Dispose()
}
关于powershell - 图标损坏与Powershell exe到ico转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60299592/