简体   繁体   中英

FolderBrowserDialog doesn't appear at all when running outside of ISE

I have the following code which should allow a user to pick a folder location for a specific file and then I will use that location to take action on the file. However, when I run the script outside of the ISE the FolderBrowserDialog doesn't appear at all. I have looked behind the powershell window and behind all other windows and it never appears.

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = "Description"
$FolderBrowser.ShowNewFolderButton = $false
$FolderBrowser.RootFolder = 'MyDocuments' 
if($FolderBrowser.ShowDialog() -eq $true){
     Do some stuff
}

Any thoughts on why it might not show up? Are there certain powershell settings that need to be turned on to have things appear from the console?

I've had this issue before in PS v2, here's how I have fixed it.

Save the code in a .ps1 file and run it like this:

powershell -STA -File "path/to/file.ps1"

ISE v2 runs in STA, and console v2 runs in MTA.

More information: Could you explain STA and MTA?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM