我正在嘗試根據所述影像的尺寸洗掉影像,但遇到了問題。
我正在嘗試洗掉長度或寬度小于 490 像素的影像。但是,我嘗試過的代碼會為每個專案引發錯誤。這是錯誤:
Remove-Item : Cannot remove item (file path): 行程無法訪問檔案
'(檔案路徑)',因為它正在被另一個行程使用。
在行:6 字符:9
洗掉專案 $_
~~~~~~~~~~~~~~
CategoryInfo : WriteError: ((file path):FileInfo) [Remove-Item], IOException
FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
這是我的代碼:
[Void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$(Get-ChildItem -Filter *.jpg).FullName | ForEach-Object {
$img = [Drawing.Image]::FromFile($_);
If (($img.Width -lt 490) -or ($img.Height -lt 490)) {
Remove-Item $_
}
}
我沒有運行任何會使用影像的明顯行程。使用 Handle64 時,它說 powershell.exe 正在使用這些檔案。任何幫助,將不勝感激!
uj5u.com熱心網友回復:
該$img物件正在使用該檔案,因此您需要在洗掉該檔案之前處理該檔案:
Add-Type -AssemblyName System.Drawing
(Get-ChildItem -Filter '*.jpg' -File).FullName | ForEach-Object {
$img = [System.Drawing.Image]::FromFile($_)
$w = $img.Width
$h = $img.Height
# get rid of the Image object and release the lock on the file
$img.Dispose()
If (($w -lt 490) -or ($h -lt 490)) {
Remove-Item -Path $_
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/468984.html
下一篇:在linux的windows上編程時使用`syscall.LINUX_REBOOT_CMD_POWER_OFF`
