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

**URL:** <https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110>\
**Category:** PowerShell Help\
**Created:** [August 31, 2016, 1:16pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110 "2016-08-31T13:16:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitheesh-babu](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@nitheesh-babu](https://forums.powershell.org/u/nitheesh-babu)\
**Post date:** [August 31, 2016, 1:16pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/1 "2016-08-31T13:16:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 31, 2016, 1:45pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/2 "2016-08-31T13:45:22Z")

</div>

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 

}
```

---

<div class="post-metadata">

**Author:** ![nitheesh-babu](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@nitheesh-babu](https://forums.powershell.org/u/nitheesh-babu)\
**Post date:** [August 31, 2016, 1:46pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/3 "2016-08-31T13:46:37Z")

</div>

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"
```

---

<div class="post-metadata">

**Author:** ![nitheesh-babu](https://avatars.discourse-cdn.com/v4/letter/n/ecae2f/32.png) [@nitheesh-babu](https://forums.powershell.org/u/nitheesh-babu)\
**Post date:** [August 31, 2016, 2:09pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/4 "2016-08-31T14:09:12Z")

</div>

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.
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 31, 2016, 2:11pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/5 "2016-08-31T14:11:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris-bakker](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chris-bakker/32/6356_2.png) [@chris-bakker](https://forums.powershell.org/u/chris-bakker)\
**Post date:** [August 31, 2016, 3:21pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/6 "2016-08-31T15:21:00Z")

</div>

Problem is simply to get the function return its value.

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

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [August 31, 2016, 3:27pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/7 "2016-08-31T15:27:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [September 1, 2016, 9:11am UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/8 "2016-09-01T09:11:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/getting-a-file-path-using-system-windows-forms-openfiledialog/7110/9 "2024-05-16T20:40:37Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


