Author Posts

August 31, 2016 at 6:46 pm

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

August 31, 2016 at 7:15 pm

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 

}

August 31, 2016 at 7:16 pm

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"

August 31, 2016 at 7:39 pm

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.

August 31, 2016 at 7:41 pm

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.

August 31, 2016 at 8:51 pm

Problem is simply to get the function return its value.

Function get-path{ RETURN (Get-FileName -initialdirectory ".\")}

August 31, 2016 at 8:57 pm

My system must have magical powers😃 I never use the begin process return stuff.

September 1, 2016 at 2:41 pm

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?