Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call FolderBrowserDialog from powershell

Similar to this question, after running the following code the browser dialog does appear with all the correct buttons, but the selection area that usally displays available folders is missing:

[void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
$d = New-Object Windows.Forms.FolderBrowserDialog
$d.ShowDialog( )
like image 649
Emperor XLII Avatar asked Jan 20 '26 02:01

Emperor XLII


2 Answers

I encountered this problem a while back and found the following COM workaround on the MSDN forums:

$app = new-object -com Shell.Application
$folder = $app.BrowseForFolder(0, "Select Folder", 0, "C:\")
if ($folder.Self.Path -ne "") {write-host "You selected " $folder.Self.Path}

http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.framework.windowsforms.controls&tid=3607557a-43b3-40bf-8276-be00526e0520&p=1

like image 152
Gordon Bell Avatar answered Jan 22 '26 19:01

Gordon Bell


I believe it is a problem with PowerShell running in a MTA Thread. You can run the CTP of Version 2 in a STA (single threaded apartment) mode and it will pull up the proper folder selection. It does pull the menu up behind the shell window though.

like image 28
Steven Murawski Avatar answered Jan 22 '26 17:01

Steven Murawski