OpenFileDialog

When working on Windows, you always have the benefit of making use of integrated Windows features. One of these is OpenFileDialog, which comes with the PresentationFramework library. The following function demonstrates a simple usage of it to retrieve and return selected files within the GUI:

function Show-OpenFileDialog
{
<#
.SYNOPSIS
Shows up an open file dialog.
.EXAMPLE
Show-OpenFileDialog
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$false, Position=0)]
[System.String]
$Title = 'Windows PowerShell',

[Parameter(Mandatory=$false, Position=1)]
[Object]
$InitialDirectory = "$HomeDocuments",

[Parameter(Mandatory=$false, Position=2)]
[System.String]
$Filter = 'PowerShell-files|*.ps1|Everything|*.*'
)
Add-Type -AssemblyName PresentationFramework

$dialog = New-Object -TypeName Microsoft.Win32.OpenFileDialog
$dialog.Title = $Title
$dialog.InitialDirectory = $InitialDirectory
$dialog.Filter = $Filter
if ($dialog.ShowDialog())
{
$dialog.FileName
}
else
{
Throw 'Nothing selected.'
}
}

#Executing
Show-OpenFileDialog

Executing this will show the file dialog with filtering directly enabled, as follows:

The selected files will be returned in the function. So, it is certainly possible to work with a foreach loop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.133