2

クリックするとボタンを持つ Windows フォームを作成しようとすると、フォルダー/ファイル参照ウィンドウが表示され、ユーザーがファイル/フォルダーを選択して [OK] をクリックすると、選択したパスを別のスクリプトの文字列として使用できます。

問題は、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
4

4 に答える 4

1

PowerShellPlus (別の PowerShell エディター) を使用してスクリプトを実行すると、同様の問題が発生しました。幸いなことに、FolderBrowserDialog を使用せずにフォルダーを要求する方法を示すこの投稿を見つけました。これは、GUI を介してさまざまな種類の入力をユーザーに求めるために作成した一連の powershell 関数で使用しているコードです。

# Show an Open Folder Dialog and return the directory selected by the user.
function Read-FolderBrowserDialog([string]$Message, [string]$InitialDirectory)
{
    $app = New-Object -ComObject Shell.Application
    $folder = $app.BrowseForFolder(0, $Message, 0, $InitialDirectory)
    if ($folder) { return $folder.Self.Path } else { return '' }
}
于 2013-05-17T20:22:15.717 に答える
1

Your code works when I try it. However I have noticed that sometimes(especially the 2nd time in a session) I use a browsewindow, it is hidden behind the PowerShell console and it seems like it's stuck. So can you try moving your powershell console to the side when it "hangs"?

Also, as a suggestion: if you're only using the form to select a folder location, I would skip it. You won't recieve the browser value until you close the form anyways, so try something like this instead:

function Get-BrowseLocation
{
    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $browse = New-Object System.Windows.Forms.FolderBrowserDialog
    $browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
    $browse.ShowNewFolderButton = $false
    $browse.Description = "Choose a directory"

    $loop = $true
    while($loop)
    {
        if ($browse.ShowDialog() -eq "OK")
        {
            $loop = $false
        } else
        {
            $res = [System.Windows.Forms.MessageBox]::Show("You clicked Cancel. Try again or exit script?", "Choose a directory", [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
            if($res -eq "Cancel")
            {
                #End script
                return
            }
        }
    }
    $browse.SelectedPath
    $browse.Dispose()
}

PS > Get-BrowseLocation
D:\
于 2013-01-27T19:10:58.483 に答える
1

Frodeが提供する機能に以下の変更を加えた場合。F、ダイアログは常に一番上に表示されます。

$topform = New-Object System.Windows.Forms.Form
$topform.Topmost = $true
$topform.MinimizeBox = $true

$loop = $true
while($loop)
{
    if ($browse.ShowDialog($topform) -eq "OK")
于 2016-05-27T06:11:07.493 に答える
0

あなたは私が直面した問題を経験していると思います。これはこの質問で対処されています

答えは、次のように に設定.ShowHelpすることを提案しています。$true

$openFileDialog = New-Object System.Windows.Forms.openFileDialog
$openFileDialog.ShowHelp = $true
$openFileDialog.ShowDialog() | Out-Null
于 2013-01-30T17:38:34.923 に答える