views:

655

answers:

3

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( )
+3  A: 

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.

Steven Murawski
I tried this first on Vista with CTP2, where I could get it working with in STA mode. On XP with V1, I was not able to get this working.
Steven Murawski
The STA limitation makes sense . . . eagerly awaiting the release version of PS v2 ;)
Emperor XLII
It might be a while :( . CTP3 is coming soon (possibly for release at PDC, but there has not been a formal announcement that I'm aware of. Unfortunately, with the CTP series, things can be added, removed, or changed, by the time it gets to release.
Steven Murawski
A: 

Just FYI, if you are looking to do Windows Forms stuff, there is one product currently out that will do windows forms for PowerShell (The Admin Script Editor) and Sapien is working on a Forms Designer (announced on the PowerScripting Podcast), which might smooth out some of these issues for your script.

Steven Murawski
+2  A: 

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

Gordon Bell
Thanks for the workaround :)
Emperor XLII