Getting a File path using System.Windows.Forms.OpenFileDialog

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Dan Potter Dan Potter 4 weeks, 1 day ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #52810
    Profile photo of nitheesh babu
    nitheesh babu
    Participant

    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

    #52839
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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 
    
    }
    
    
    #52841
    Profile photo of nitheesh babu
    nitheesh babu
    Participant

    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"
    
    #52850
    Profile photo of nitheesh babu
    nitheesh babu
    Participant

    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.
    
    #52853
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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.

    #52884
    Profile photo of Chris Bakker
    Chris Bakker
    Participant

    Problem is simply to get the function return its value.

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

    #52897
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

    #53039
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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?

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.