Powershell OpenFileDialog留在前面

时间:2014-08-18 19:06:29

标签: .net powershell openfiledialog

我有PowerShell代码,使用OpenFileDialog让用户选择文件。

我第一次执行代码时,对话框出现在顶部,但第二次,它出现在powershell GUI后面。

如何将OpenFileDialog放在前面?

<# courtesy of http://blog.danskingdom.com/powershell-multi-line-input-box-dialog-open-file-dialog-folder-browser-dialog-input-box-and-message-box/ #>
function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [string]$Filter = "All files (*.*)|*.*",[switch]$AllowMultiSelect)
{
    Add-Type -AssemblyName System.Windows.Forms

    $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $openFileDialog.Title = $WindowTitle

    if (![string]::IsNullOrWhiteSpace($InitialDirectory)){
        $openFileDialog.InitialDirectory = $InitialDirectory
    }

    $openFileDialog.Filter = $Filter

    if ($AllowMultiSelect){
        $openFileDialog.Multiselect = $true
    }

    $openFileDialog.ShowHelp = $true

    $openFileDialog.ShowDialog() > $null

    if ($AllowMultiSelect){
        return $openFileDialog.FileNames
    }
    else{
        return $openFileDialog.FileName
    }

}


$filePath = Read-OpenFileDialog -WindowTitle "Select Executable" -InitialDirectory 'C:\' -Filter "Executable files (*.exe)|*.exe"

if (![string]::IsNullOrEmpty($filePath)){
    Write-Host "You selected the file: $filePath"
}
else{
    "You did not select a file."
}

0 个答案:

没有答案