我们不知道您的 GUI 如何显示的细节,但是,假设您使用的是在PowerShell代码中构建的 WinForms GUI,您的问题可能是您的 GUI 构建代码如何从您以后想要的文件夹中的文件中加载图像删除。
值得注意的是,如果你使用类似的东西:
[Bitmap]::new(<file-path>)
[System.Drawing.Image]::FromFile(<file-path>)
指定的文件显然在 PowerShell 会话的剩余时间内保持打开状态,您将无法删除您的文件夹。
解决方案是在内存中创建一个新的图像实例来复制从文件加载的图像,然后释放从文件加载的图像,这会释放对基础文件的锁定,如此 [C#]回答。
这是一个演示该方法的最小脚本demo.ps1
:
demo.ps1
:
param([switch] $SelfDestruct)
# Load the WinForms assembly.
Add-Type -AssemblyName System.Windows.Forms
# Create the form.
$form = New-Object system.Windows.Forms.Form -Property @{
ClientSize = New-Object System.Drawing.Point 400,100
Text = "Dialog"
}
# Add a PictureBox control that loads its image from 'demo.png'
$form.Controls.Add((New-Object System.Windows.Forms.PictureBox -Property @{
Image = & {
# Load the image from file in the same folder as a script into
# a temporary variable.
$tmpImg = [System.Drawing.Image]::FromFile((Join-Path $PSScriptRoot 'demo.png'))
# Create an in-memory copy of the image.
New-Object System.Drawing.Bitmap $tmpImg
# Dispose of the from-file image, which releases the file.
$tmpImg.Dispose()
}
Location = New-Object System.Drawing.Point 10, 10
}))
# Show the form and wait for the use to close it.
$null = $form.ShowDialog()
if ($SelfDestruct) { # Remove the running script's entire folder.
if ("$($PWD.Path)\" -like "$PSScriptRoot\*") { #"
Push-Location C:\ # must switch to different dir. before deleting.
}
# Remove the entire folder.
Remove-Item -literalpath $PSScriptRoot -Recurse -Force
exit
}
这是一个通过事件处理程序通过按钮单击触发删除的变体:
param([switch] $SelfDestruct)
Add-Type -AssemblyName System.Windows.Forms
function remove-OwnFolder {
# If the current dir. is in the subtree of the folder to delete,
# we must switch to different dir. before deleting.
if ("$($PWD.Path)\" -like "$PSScriptRoot\*") { #"
Push-Location C:\
}
# Remove the script's parent folder as a whole.
Remove-Item -literalpath $PSScriptRoot -Recurse -Force
}
# Create the form.
$form = New-Object system.Windows.Forms.Form -Property @{
ClientSize = New-Object System.Drawing.Point 400,100
Text = "Dialog"
}
# Add a PictureBox control that loads its image from 'demo.png'
$form.Controls.Add((New-Object System.Windows.Forms.PictureBox -Property @{
Image = & {
# Load the image from file in the same folder as a script into
# a temporary variable.
$tmpImg = [System.Drawing.Image]::FromFile((Join-Path $PSScriptRoot 'demo.png'))
# Create an in-memory copy of the image.
New-Object System.Drawing.Bitmap $tmpImg
# Dispose of the from-file image, which releases the file.
$tmpImg.Dispose()
}
Location = New-Object System.Drawing.Point 10, 10
}))
# Add a button that will trigger the self-destruction
$btnSelfDestruct = New-Object system.Windows.Forms.Button -Property @{
Text = "Submit"
Location = New-Object System.Drawing.Point 160, 60
}
$form.Controls.Add($btnSelfDestruct)
# Add the button-click event handler.
$btnSelfDestruct.Add_Click({
$form.Close()
if ($SelfDestruct) {
remove-OwnFolder
}
exit
})
# Show the form and wait for the use to close it.
$null = $form.ShowDialog()