System.Windows.Forms.OpenFileDialog always ontop

Hello All

when using the following code to present a browse for file dialog to a user, sometimes the windows will open behind another windows and therefore cannot be seen. I know I have seen a tip somewhere on how to prevent this from happening (i.e. method etc) but I cannot locate it at the moment. Does someone know how to make sure the windows always opens on top?

Thanks all

 

function Read-OpenFileDialog ( `
[string]$WindowTitle="Please select a file", [string]$InitialDirectory="C:\", `
[string]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect=$false)
{
Add-Type -AssemblyName System.Windows.Forms
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.Title = $WindowTitle
$openFileDialog.InitialDirectory = $InitialDirectory
$openFileDialog.Filter = $Filter
if ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }
$openFileDialog.ShowHelp = $true
$openFileDialog.ShowDialog() | out-null
if ($AllowMultiSelect) { return $openFileDialog.Filenames } else { return $openFileDialog.Filename }
}
  • Hello EBrant, yes because you have ShowHelp set to true a different form is being drawn. If you change this to false you will see that the window actually looks different. This window will popup everytime it is created.

    So try the following code, I tested this on Windows 8 and Server 2008R2, both with PowerShell v3. I have also improved your function by including a param() block, fixed the incorrect bracket on the first line and removed the line break character, which you should (almost) never use:

    function Read-OpenFileDialog {
    param (
    [string]$WindowTitle="Please select a file",
    [string]$InitialDirectory="C:\",
    [string]$Filter = "All files (*.*)|*.*",
    [switch]$AllowMultiSelect=$false
    )
    Add-Type -AssemblyName System.Windows.Forms
    $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $openFileDialog.Title = $WindowTitle
    $openFileDialog.InitialDirectory = $InitialDirectory
    $openFileDialog.Filter = $Filter
    if ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }
    $openFileDialog.ShowHelp = $false
    $openFileDialog.ShowDialog() | out-null
    if ($AllowMultiSelect) { return $openFileDialog.Filenames } else { return $openFileDialog.Filename }
    }
  • Hello Jaap

    Unfortunately there is a bit of a gotcha here and that is this

    I am using PowerShell v2 (not sure if the same issue is present in PowerShell v3)

    if you remove the line

    $openFileDialog.ShowHelp = $true

    it works as expected when run from the ISE or PowerGUI etc, however if you run from the PowerShell console it appear to hang i.e. prompt does not return and no dialog displayed. I also tried running PowerShell in single threaded mode -sta to overcome this no cigar

    So it looks like if you are running form the GUI set $openFileDialog.ShowHelp to $false and if you are running from the shell set to $true

    I got around this issue by using

    if

     

     

    ($host.name -ne "ConsoleHost") {......}

    Check it out see what you think (try in PosH 3 too), I see you posted a useful tip on the above subject (emailed), you may want to adjust  slightly depending on your finding re the above.

    Thanks again for your help Jaap

    Ernie

  • Jaap

    Picking up on your helpful post, I just adjusted like so, now works OK in console or GUI

     

    Add-Type -AssemblyName System.Windows.Forms

    if ($Host.name -eq "ConsoleHost") {$ShowHelp = $true}

    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false
    ShowHelp = $ShowHelp
    }

    [void]$FileBrowser.ShowDialog()
    $FileBrowser.FileNames
  • Great, thanks for letting us know EBrant. Thank for doing some extensive testing, PowerShell 2 is not very common in my environment anymore so I did not get around to testing it there.