作成 2010.01.05
更新 2011.11.27
更新 2011.11.27
PowerShell で SaveFileDialog もどき
なんか表示できなかったので。
本体
Function Show-SaveFileDialog([String] $default_filename = ""){ [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms") $STR_OPEN_TITLE = "ファイルを保存する" $STR_OPEN_BUTTON = "保存" $STR_CANCEL_BUTTON = "キャンセル" $STR_OPEN_DIR = "保存する場所:" $STR_OPEN_NAME = "ファイル名:" $STR_FOLDER_UP = "↑" $STR_SAVE_QUESTION = "指定されたファイルは既にありますが、上書きしますか?" $SAVE_PATH = $null $file_dialog = New-Object Windows.Forms.Form $file_dialog.Width = 505 $file_dialog.Height = 274 $file_opendir_label = New-Object Windows.Forms.Label $file_opendir_combo = New-Object Windows.Forms.ComboBox $file_opendir_up = New-Object Windows.Forms.Button $file_open_list = New-Object Windows.Forms.ListBox $file_open_button = New-Object Windows.Forms.Button $file_cancel_button = New-Object Windows.Forms.Button $file_openname_label = New-Object Windows.Forms.Label $file_openname_text = New-Object Windows.Forms.TextBox $file_opendir_label.AutoSize = $true $file_opendir_label.Location = New-Object Drawing.Point(10,10) $file_opendir_label.Text = $STR_OPEN_DIR $file_open_button.Text = $STR_OPEN_BUTTON $file_open_button.Location = New-Object Drawing.Point(($file_dialog.Width - $file_open_button.Width - 15), ($file_dialog.Height - ($file_open_button.Height)*2 - 41)) $file_open_button.Add_Click({ if($file_openname_text.Text.Length -lt 1) { [String] $temp_item = $file_open_list.SelectedItem if($temp_item -match "\\$") { $temp_item1 = $file_opendir_combo.Text.Trim("\") + "\" + $temp_item.Trim("\") if($file_opendir_combo.Items.IndexOf($temp_item1) -lt 0) { $file_opendir_combo.Items.Insert($file_opendir_combo.Items.IndexOf($file_opendir_combo.Text) + 1 , $temp_item1) } $file_opendir_combo.Text = $temp_item1 return $null } else { $file_openname_text.Text = $temp_item } } $file_path = $file_opendir_combo.Text.Trim("\") + "\" + $file_openname_text.Text if(Test-Path $file_path) { if([Windows.Forms.MessageBox]::Show($STR_SAVE_QUESTION,$STR_OPEN_TITLE,[Windows.Forms.MessageBoxButtons]::YesNo,[Windows.Forms.MessageBoxIcon]::Question) -eq [Windows.Forms.DialogResult]::No) { return $null } } $SAVE_PATH = $file_path $file_dialog.Close() }) $file_cancel_button.Text = $STR_CANCEL_BUTTON $file_cancel_button.Location = New-Object Drawing.Point($file_open_button.left, ($file_open_button.top + $file_open_button.height + 6)) $file_opendir_up.AutoSize = $false $file_opendir_up.Font = New-Object Drawing.Font($file_opendir_up.Font.FontFamily, 9) $file_opendir_up.Text = $STR_FOLDER_UP $file_opendir_up.Size = New-Object Drawing.Size(20, 20) $file_opendir_up.Location = New-Object Drawing.Point(($file_dialog.Width - $file_opendir_up.Width - 15), ($file_opendir_label.Top - 4)) $file_opendir_up.Add_Click({ $temp_dirname = $file_opendir_combo.Text if($temp_dirname -match "(.+)\\[^\\]+$") { $temp_dirname = $matches[1] if($temp_dirname.Length -lt 3){ $temp_dirname += "\" } } $file_opendir_combo.Text = $temp_dirname }) $file_opendir_combo.Location = New-Object Drawing.Point(($file_opendir_label.Left + $file_opendir_label.PreferredWidth + 5),($file_opendir_label.Top - 4)) $file_opendir_combo.Width = $file_opendir_up.Left - $file_opendir_combo.Left - 8 $file_drives = Get-PSDrive -PSProvider "FileSystem" $file_combo_array = @() foreach($temp_item in $file_drives) { $file_combo_array += $temp_item.Root if($temp_item.CurrentLocation.Length -gt 0) { $temp_str = $temp_item.Root foreach($temp_item1 in $temp_item.CurrentLocation.Split("\")) { $file_combo_array += ($temp_str + $temp_item1) $temp_str += $temp_item1 + "\" } } } $file_opendir_combo.Items.AddRange($file_combo_array) $file_opendir_combo.Add_TextChanged({ $file_open_list.Items.Clear() if(Test-Path $file_opendir_combo.Text) { $temp_array = Get-ChildItem -Path $file_opendir_combo.Text if($temp_array -ne $null) { $temp_array1 = @() foreach($temp_item in $temp_array) { if($temp_item.Mode -match "^d") { $temp_array1 += ($temp_item.Name + "\") } else { $temp_array1 += $temp_item.Name } } $file_open_list.Items.AddRange( $temp_array1 ) } } }) $file_opendir_combo.Text = (Get-Location).Path $file_open_list.Location = New-Object Drawing.Point($file_opendir_label.Left, ($file_opendir_label.Top + $file_opendir_combo.Height)) $file_open_list.Width = $file_opendir_up.Width + $file_opendir_up.Left - $file_opendir_label.Left $file_open_list.Height = $file_open_button.Top - $file_open_list.Top $file_open_list.Add_SelectedValueChanged({ [String] $temp_item = $file_open_list.SelectedItem if(!($temp_item -match "\\$")) { $file_openname_text.Text = $temp_item } }) $file_open_list.Add_DoubleClick({ [String] $temp_item = $file_open_list.SelectedItem if($temp_item -match "\\$") { $temp_item1 = $file_opendir_combo.Text.Trim("\") + "\" + $temp_item.Trim("\") if($file_opendir_combo.Items.IndexOf($temp_item1) -lt 0) { $file_opendir_combo.Items.Insert($file_opendir_combo.Items.IndexOf($file_opendir_combo.Text) + 1 , $temp_item1) } $file_opendir_combo.Text = $temp_item1 } else { $file_openname_text.Text = $temp_item $file_dialog.Close() } }) $file_openname_label.AutoSize = $true $file_openname_label.Location = New-Object Drawing.Point($file_opendir_label.Left, ($file_open_button.Top + 4)) $file_openname_label.Text = $STR_OPEN_NAME $file_openname_text.Location = New-Object Drawing.Point(($file_opendir_label.Left + $file_opendir_label.PreferredWidth + 5), $file_open_button.Top) $file_openname_text.Width = $file_open_button.Left - $file_openname_text.Left - 10 $file_openname_text.Text = $default_filename $file_dialog.Text = $STR_OPEN_TITLE $file_dialog.HelpButton = $false $file_dialog.FormBorderStyle = [Windows.Forms.FormBorderStyle]::FixedDialog $file_dialog.MaximizeBox = $false $file_dialog.MinimizeBox = $false $file_dialog.AcceptButton = $file_open_button $file_dialog.CancelButton = $file_cancel_button $file_dialog.StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen $file_dialog.Controls.Add($file_opendir_label) $file_dialog.Controls.Add($file_opendir_combo) $file_dialog.Controls.Add($file_opendir_up) $file_dialog.Controls.Add($file_open_list) $file_dialog.Controls.Add($file_openname_label) $file_dialog.Controls.Add($file_openname_text) $file_dialog.Controls.Add($file_open_button) $file_dialog.Controls.Add($file_cancel_button) $null = $file_dialog.ShowDialog() return $SAVE_PATH }
実行方法
$file_name = Show-SaveFileDialog "savefile.txt"
タグ: PowerShell