Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Show-FolderBrowser
- {
- Param ( [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Title='Browse For Folder',
- [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$SelectedPath = $(Split-Path $psISE.CurrentFile.FullPath),
- [Parameter(Mandatory=0,ValueFromPipeline=1)][switch]$ShowNewFolderButton)
- $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog;
- $FolderBrowser.Description = $Title
- $FolderBrowser.ShowNewFolderButton = $ShowNewFolderButton
- $FolderBrowser.SelectedPath = $SelectedPath
- $sp = $null
- $caller = [System.Windows.Forms.NativeWindow]::new()
- $caller.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
- if (($FolderBrowser.ShowDialog($caller)) -eq [System.Windows.Forms.DialogResult]::OK.value__)
- {
- $sp = $FolderBrowser.SelectedPath
- }
- #Cleanup
- $FolderBrowser.Dispose()
- return $sp
- }
- function Show-FileBrowser
- {
- Param ( [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Title='Browse For File',
- [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$InitialDirectory = $(Split-Path $psISE.CurrentFile.FullPath),
- [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Filter = 'All files (*.*)|*.*'
- )
- $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
- $FileBrowser.Title = $Title
- $FileBrowser.InitialDirectory = $InitialDirectory
- $FileBrowser.Filter = $Filter
- $fn = $null
- $caller = [System.Windows.Forms.NativeWindow]::new()
- $caller.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
- if (($FileBrowser.ShowDialog($caller)) -eq [System.Windows.Forms.DialogResult]::OK.value__)
- {
- $fn = $FileBrowser.FileName
- }
- #Cleanup
- $FileBrowser.Dispose()
- return $fn
- }
- function Show-SaveFileBrowser
- {
- Param ( [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Title='Save File',
- [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$InitialDirectory = $(Split-Path $psISE.CurrentFile.FullPath),
- [Parameter(Mandatory=0,ValueFromPipeline=1)][string]$Filter = 'All files (*.*)|*.*'
- )
- $SaveBrowser = New-Object System.Windows.Forms.SaveFileDialog
- $SaveBrowser.Title = $Title
- $SaveBrowser.InitialDirectory = $InitialDirectory
- $SaveBrowser.Filter = $Filter
- $fn = $null
- $caller = [System.Windows.Forms.NativeWindow]::new()
- $caller.AssignHandle([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
- if (($SaveBrowser.ShowDialog($caller)) -eq [System.Windows.Forms.DialogResult]::OK.value__)
- {
- $fn = $SaveBrowser.FileName
- }
- #Cleanup
- $SaveBrowser.Dispose()
- return $fn
- }
- $documents = [Environment]::GetFolderPath("MyDocuments")
- Clear-Host
- Show-FolderBrowser -SelectedPath $documents -ShowNewFolderButton
- Show-FileBrowser -InitialDirectory $documents -Filter 'PowerShell files (*.ps1)|*.ps1'
- Show-SaveFileBrowser -InitialDirectory $documents -Filter 'PowerShell files (*.ps1)|*.ps1'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement