我试图创建一个窗口窗体,单击时有一个按钮将显示一个文件夹/文件浏览窗口,然后用户选择文件/文件夹并单击确定,然后我可以将所选路径用作另一个脚本的字符串。
问题是,当我通过 PowerGUI(powershell 脚本应用程序)运行它时,它工作正常,但是当我通过 windows powershell 运行它时,它在加载浏览对话框时挂起,任何人都见过这个,或者看到我做错了什么或得到了替代方案,任何帮助将不胜感激。
cls
$button = $browse = $form = 0
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$browse = new-object system.windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.selectedPath = "C:\"
$browse.Description = "Choose a directory"
$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Choose Directory"
$button1.Add_Click({$browse.ShowDialog()})
$button1.left = 20
$button1.top = 20
$form = New-Object system.windows.forms.Form
$form.controls.add($button1)
$form.ShowDialog()
$form.Dispose()
$browse.SelectedPath