gpt4 book ai didi

powershell - 进程锁定临时文件。我该如何解决?

转载 作者:行者123 更新时间:2023-12-02 23:58:09 30 4
gpt4 key购买 nike

作为创建用于处理某些重复性任务的GUI的一部分,我为八球功能做了准备。作为此功能的一部分,我使用背景图像和嵌入脚本中并写入$ env:tmp的图标。

从GUI退出后,我需要进行清理以删除临时文件。但是,即使PowerShell已终止,该脚本也无法在PowerShell运行时删除背景图像。

Remove-Item : Cannot remove item C:\Users\USERNAME\AppData\Local\Temp\11a8093b2817a3e1ff135fec4fe01320.jpg: The process cannot access the file 'C:\Users\USERNAME\AppData\Local\Temp\1
1a8093b2817a3e1ff135fec4fe01320.jpg' because it is being used by another process.
At C:\Users\USERNAME\Desktop\8ball.ps1:89 char:1
+ Remove-Item "$env:temp\11a8093b2817a3e1ff135fec4fe01320.jpg"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Users\USERN~...fec4fe01320.jpg:FileInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

有什么“简单”的方法可以解决这个问题吗?
$MagicEightBallPicture = ""
$ContentPicture = [System.Convert]::FromBase64String($MagicEightBallPicture)
(Set-Content -Path $env:temp\11a8093b2817a3e1ff135fec4fe01320.jpg -Value $ContentPicture -Encoding Byte)

$MagicEightBallIcon = ""
$ContentIcon = [System.Convert]::FromBase64String($MagicEightBallIcon)
(Set-Content -Path $env:temp\11a8093b2817a3e1ff135fec4fe01320.ico -Value $ContentIcon -Encoding Byte)

$PossibleAnswers = @("It is certain",`
"It is decidedly so",`
"Without a doubt",`
"Yes, definitely",`
"You may rely on it",`
"As I see it, yes",`
"Most likely",`
"Outlook good",`
"Yes",`
"Signs point to yes",`
"Reply hazy try again",`
"Ask again later",`
"Better not tell you now",`
"Cannot predict now",`
"Concentrate and ask again",`
"Dont count on it",`
"My reply is no",`
"My sources say no",`
"Outlook not so good",`
"Very doubtful")

Function RollTheBall {

$TheAnswer = Get-Random -Count 1 -InputObject $PossibleAnswers
$TheAnswerBox.Text = $TheAnswer

}

Try {

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Image = [System.Drawing.Image]::Fromfile("$($env:temp)\11a8093b2817a3e1ff135fec4fe01320.jpg")
$Icon = New-Object System.Drawing.Icon ("$($env:temp)\11a8093b2817a3e1ff135fec4fe01320.ico")

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Magic Eight Ball"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.FormBorderStyle = 'Fixed3D'
$Form.SizeGripStyle = "Hide"
$Form.ShowInTaskbar = $False
$Form.StartPosition = "CenterScreen"
$Form.Icon = $Icon
$Form.BackgroundImage = $Image
$Form.BackgroundImageLayout = "None"
$Form.Width = $Image.Width
$Form.Height = $Image.Hight

$Form.Size = New-Object System.Drawing.Size(300,200)

$TheAnswerBox = New-Object System.Windows.Forms.TextBox
$TheAnswerBox.Location = New-Object System.Drawing.Size(10,10)
$TheAnswerBox.Size = New-Object System.Drawing.Size(270,20)
$TheAnswerBox.TextAlign = [System.Windows.Forms.HorizontalAlignment]::Center
$TheAnswerBox.add_GotFocus({[System.Windows.Forms.SendKeys]::Send("{tab}")})
$Form.Controls.Add($TheAnswerBox)

$ButtonRollTheBall = New-Object System.Windows.Forms.Button
$ButtonRollTheBall.Location = New-Object System.Drawing.Size(10,40)
$ButtonRollTheBall.Size = New-Object System.Drawing.Size(130,20)
$ButtonRollTheBall.Text = "Help?!"
$ButtonRollTheBall.Add_Click({RollTheBall})
$Form.Controls.Add($ButtonRollTheBall)

$ButtonExit = New-Object System.Windows.Forms.Button
$ButtonExit.Location = New-Object System.Drawing.Size(150,40)
$ButtonExit.Size = New-Object System.Drawing.Size(130,20)
$ButtonExit.Text = "Exit"
$ButtonExit.Add_Click({$Form.Close()})
$Form.Controls.Add($ButtonExit)

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

} Finally {

Remove-Item "$env:temp\11a8093b2817a3e1ff135fec4fe01320.jpg"
Remove-Item "$env:temp\11a8093b2817a3e1ff135fec4fe01320.ico"

}

由于可能的版权和剪切大小,我已删除了嵌入式图片。

最佳答案

由于从[System.Drawing.Image]::Fromfile返回的Imagedisposable,因此您必须先对其进行Dispose清理资源,然后再将其删除:

$Image.Dispose()

关于powershell - 进程锁定临时文件。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37391758/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com