简体   繁体   English

在ISE外部运行时,根本不显示FolderBrowserDialog

[英]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. 但是,当我在ISE之外运行脚本时,FolderBrowserDialog根本不会出现。 I have looked behind the powershell window and behind all other windows and it never appears. 我一直在Powershell窗口后面和所有其他窗口后面看,但从未出现过。

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? 是否需要打开某些Powershell设置才能使控制台中显示内容?

I've had this issue before in PS v2, here's how I have fixed it. 我在PS v2中曾遇到过此问题,这是我已解决的问题。

Save the code in a .ps1 file and run it like this: 将代码保存在.ps1文件中,然后像这样运行它:

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

ISE v2 runs in STA, and console v2 runs in MTA. ISE v2在STA中运行,而控制台v2在MTA中运行。

More information: Could you explain STA and MTA? 详细信息: 您能解释一下STA和MTA吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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