1

我一直在尝试让 powershell 或批处理脚本在我完成后删除一个包含我所有脚本的文件夹。最初我尝试Remove-Item -Path "C:\Tool" -Recurse -Force了如果作为脚本运行在C:\Tool. 当从其中的脚本运行时,它会抱怨文件正在使用中。经过一些研究,我发现&cmd.exe /c rd /s /q "C:\Tool"哪个效果更好,但即使我关闭了 GUI,该命令也不会删除正在使用的 img 文件/文件夹。

从 USB 驱动器启动时,上述两个命令都能完美运行。

以前我在临时文件夹中创建了第二个脚本,它将删除所有文件,然后删除自身。我正在寻找一种新方法来简化我正在研究的新设计。我希望脚本可以从 USB 驱动器内部C:\Tool或从 USB 驱动器运行。

控制流程是这样的:
1)脚本加载所有功能
2)显示GUI(包含imgs)
3)按下按钮
4)关闭GUI
5)删除包含脚本的文件夹

如前所述,第 5 步是我的问题。并非所有文件都被尝试的命令和命令的变体删除。

我希望步骤 5 能够正常工作,无论是从 GUI 上的按钮调用命令、它作为脚本的一部分自动运行,还是其他位置(如 USB)中的脚本调用它来删除文件夹C:\Tool

4

1 回答 1

0

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

值得注意的是,如果你使用类似的东西:

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

指定的文件显然在 PowerShell 会话的剩余时间内保持打开状态,您将无法删除您的文件夹。

解决方案是在内存中创建一个新的图像实例来复制从文件加载的图像,然后释放从文件加载的图像,这会释放对基础文件的锁定,如此 [C#]回答

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

  • 将其保存到其自己的临时丢弃文件夹中。

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

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

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()
于 2018-08-23T19:38:20.303 回答