BrowseForFolder-Dialog: center and make TopMost
You have to specify the owner handle.
$handle = [System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle$getfolder = New-Object -ComObject Shell.Application$foldername = $getfolder.BrowseForFolder([int]$handle, "Text", 16, "")
But it is a mixture of COM and .NET. I recommend the following solution
$win32WindowDefinition = @"using System;using System.Windows.Forms;public class Win32Window : IWin32Window{ public Win32Window(IntPtr handle) { Handle = handle; } public IntPtr Handle { get; private set; }}"@Add-Type -TypeDefinition $win32WindowDefinition -ReferencedAssemblies System.Windows.Forms.dll $ownerHandle = New-Object Win32Window -ArgumentList ([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)$folderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog$dialogResult = $folderBrowserDialog.ShowDialog($ownerHandle)if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK) { $folderName = $folderBrowserDialog.SelectedPath}
If you can use FolderBrowserDialog
, to show it as top-most and at the center of screen, it's enough to pass a TopMost
form to its ShowDialog
method.
C# Example
var f = new FolderBrowserDialog();f.ShowDialog(new Form() { TopMost = true}); this.Activate();
Powershell Example
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")$folder = New-Object System.Windows.Forms.FolderBrowserDialog$form = New-Object System.Windows.Forms.Form -property @{TopMost = $True}$folder.ShowDialog($form)