Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Show-SelectFileDialog
- {
- [CmdletBinding()]
- Param
- (
- [Parameter (
- Mandatory,
- Position = 0
- )]
- [ValidateNotNullOrEmpty()]
- [string]
- $TopDir,
- # extension examples = "*", "*.*", "log", ".txt", "*.csv"
- [Parameter (
- Position = 1
- )]
- [string]
- $FileType = '*.*'
- )
- switch ($FileType)
- {
- # extensionless files can't be shown on their own
- # so they are included with the ALL files group
- {$_ -in ('.', '*.', '', '*', '.*', '*.*')}
- {
- $FilterString = "All files (*.*)|*.*"
- break
- }
- {$_.StartsWith('*.')}
- {
- $TypeName = $_.TrimStart('*.').ToUpper()
- $FilterString = "$TypeName files ($FileType)|$FileType|All files (*.*)|*.*"
- break
- }
- {$_.StartsWith('.')}
- {
- $TypeName = $_.TrimStart('.').ToUpper()
- $FilterString = "$TypeName files (*$FileType)|*$FileType|All files (*.*)|*.*"
- break
- }
- default
- {
- $TypeName = $_.ToUpper()
- $FilterString = "$TypeName files (*.$FileType)|*.$FileType|All files (*.*)|*.*"
- }
- }
- [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") |
- Out-Null
- $SelectFileDialog = New-Object System.Windows.Forms.OpenFileDialog
- $SelectFileDialog.InitialDirectory = $TopDir
- $SelectFileDialog.Filter = $FilterString
- $SelectFileDialog.Title = 'Please select a file and then click [Open]'
- # finally show the dialog
- $SelectFileDialog.ShowDialog() |
- Out-Null
- # send the filename to output
- $SelectFileDialog.FileName
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement