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

Welcome Forums General PowerShell Q&A Getting a File path using System.Windows.Forms.OpenFileDialog

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
2 years, 3 months ago.

  • Author
    Posts
  • #52810

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 21
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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.
    
    • #52884

      Participant
      Points: 1
      Rank: Member

      Problem is simply to get the function return its value.

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

  • #52853

    Participant
    Points: 21
    Rank: Member

    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.

  • #52897

    Participant
    Points: 21
    Rank: Member

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

  • #53039

    Participant
    Points: 21
    Rank: Member

    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?

The topic ‘Getting a File path using System.Windows.Forms.OpenFileDialog’ is closed to new replies.