首页 > 解决方案 > 如何在选择文件窗口上按取消并在 powershell 中重新启动程序

问题描述

我的程序中有一个按钮,可以让用户从他们的计算机中选择一个文件,但是我想这样做,所以当他们按下取消按钮时,它会重新启动整个程序。对此的任何帮助将不胜感激,因为我已经为此苦苦挣扎了一段时间。这是我目前所拥有的......

function SelectDrive($title){
  if($global:FirstDrive -eq $null){
  $DriveSelection = New-Object System.Windows.Forms.FolderBrowserDialog
  $DriveSelection.Description = $title
# $DriveSelection.rootfolder = "MyComputer"
  [void]$DriveSelection.ShowDialog()  
  $global:FirstDrive = $DriveSelection.SelectedPath
    }
       else{
          removeAll 
          initVal
          $FillError.text = ""     
          $mainForm.controls.AddRange(@($StartUSB,$StartDualMedia,$SelectOne,$USBLabel,$2USBLabel))
          $mainForm.refresh()
}

}

标签: formspowershell

解决方案


$ClickedButton = $DriveSelection.ShowDialog()
if ($ClickedButton -eq 'Cancel') {
    Start-Process powershell.exe -ArgumentList "-File $($MyInvocation.MyCommand.Definition)"
    Exit
}

推荐阅读