且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有脚本Powershell的自我删除文件夹

更新时间:2023-12-04 20:05:10

我们不知道GUI的显示方式的具体细节,但是,假设您使用的是在 PowerShell 中构建的WinForms GUI,>代码,您的问题可能是您的GUI构造代码如何从以后要删除的文件夹中的文件中加载图像.

We don't know the specifics of how your GUI is displayed, but, assuming you're using a WinForms GUI constructed in PowerShell code, your problem may be how your GUI construction code loads images from files in the folder that you later want to delete.

值得注意的是,如果您使用类似以下内容的话:

Notably, if you use something like:

[Bitmap]::new(<file-path>)
[System.Drawing.Image]::FromFile(<file-path>)

指定的文件显然会在PowerShell会话的其余部分保持打开状态,并且您将无法删除该文件夹.

the specified file apparently stays open for the remainder of the PowerShell session, and you won't be able to delete your folder.

解决方案是在内存中创建一个 new 图像实例,以复制从文件中加载的图像,然后处置从文件加载的图片的em> ,它释放了对底层文件的锁定,如此[C#]答案.

The solution is to create a new image instance in memory that copies the loaded-from-file image, and to then dispose of the loaded-from-file image, which releases the lock on the underlying file, as demonstrated in this [C#] answer.

这是一个最小的脚本 demo.ps1 ,它演示了该方法:

Here's a minimal script, demo.ps1, that demonstrates the approach:

  • 将其保存到自己的临时临时存放文件夹中.

  • Save it to its own, temporary, throw-away folder.

将名为 demo.png 的小图像文件复制到同一文件夹中.

Copy a small image file named demo.png into the same folder.

然后以< temp-folder>/demo -SelfDestruct 调用它以查看其运行情况;需要指定
-SelfDescript 是一种预防措施,因为意外调用会清除脚本所在的整个文件夹.

Then invoke it as <temp-folder>/demo -SelfDestruct to see it in action; the need to specify
-SelfDescript is a precaution, given that accidental invocation would wipe out the entire folder in which the script lives.

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
}


这是一个变体,它通过事件处理程序通过按钮点击触发删除:


Here's a variant that triggers the removal by button click, via an event handler:

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()