Hi,
I am trying to get a file path using System.Windows.Forms.OpenFileDialog and facing following issue:
Issue: If I call the function(with the form to open a file) it returns the selected file path as expected. But if I try to call the same function within another function it does not get the selected file path.
What could be the issues. Thanks for the assistance.
Code I am trying:
Add-Type -AssemblyName System.Windows.Forms
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialdirectory
$OpenFileDialog.filter = “TXT (*.txt)| *.txt”
$OpenFileDialog.ShowHelp = $true
$OpenFileDialog.ShowDialog() | out-null
$OpenFileDialog.filename
}
Function get-path()
{
$inputfilepath = Get-FileName -initialdirectory "."
$inputfilepath >> .\filepath.txt
}
get-path
not sure why you would double up the functions for the same result but yes it does work.
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
Function Get-FileName{
param($initialdirectory)
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialdirectory
$OpenFileDialog.filter = "TXT (*.txt)| *.txt"
$OpenFileDialog.ShowHelp = $true
$OpenFileDialog.ShowDialog() | out-null
$OpenFileDialog.filename
}
Function get-path{
$inputfilepath = Get-FileName -initialdirectory ".\"
$inputfilepath
}
I am calling this function within another object’s action field
$btn = New-Object System.Windows.Forms.Button
#Read Button event
$btn.add_click(
{
$inputfilepath = Get-FileName -initialdirectory ".\"
#here I will add steps which use $inputfilepath as input
}
)
$btn.location= New-Object System.Drawing.Size(35,35)
$btn.Text = "Import"
Thanks Dan,
It is still not working when I nest the function
If I call the function directly, it works fine. But my script need it to be called from within another function.
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
Function Get-FileName{
param($initialdirectory)
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialdirectory
$OpenFileDialog.filter = "TXT (*.txt)| *.txt"
$OpenFileDialog.ShowHelp = $true
$OpenFileDialog.ShowDialog() | out-null
$OpenFileDialog.filename
}
Function get-path{
$inputfilepath = Get-FileName -initialdirectory ".\"
$inputfilepath
}
get-path
# $inputfilepath is coming with blank.
I don’t know, that works for me. I type get-path openfiledialog pops up I select a txt file and the path is returned.
Problem is simply to get the function return its value.
Function get-path{ RETURN (Get-FileName -initialdirectory ".")}
My system must have magical powers😃 I never use the begin process return stuff.
about_return
In Windows PowerShell, the results of each statement are returned as
output, even without a statement that contains the Return keyword.
Languages like C or C# return only the value or values that are specified
by the Return keyword.
Doubling up the functions that “return” the same value would be poor coding in my opinion.
function add{1+1};function simplemath{add};simplemath #whyintheworld?