$openfiledialog1.InitialDirectory not opening in correct folder

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ocsscott
Posts: 47
Joined: Thu Dec 17, 2015 7:24 am

$openfiledialog1.InitialDirectory not opening in correct folder

Post by ocsscott » Thu Jun 27, 2019 6:01 am

$openfiledialog1.InitialDirectory = "C:\Download\cert"
$openfiledialog1.Filter = "Config|*.cfg"

if ($openfiledialog1.ShowDialog() -eq 'OK')
{
$SelectedPath = $openfiledialog1.FileName

Ive used several hard coded attempt but when running it opens in the same folder as my exe not where I specify any ideas?

When

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:pSS 2019 current build
32 or 64 bit version of product:64
Operating system:win 10 1607
32 or 64 bit OS: win 10 64bit

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by davidc » Thu Jun 27, 2019 7:19 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by davidc » Thu Jun 27, 2019 7:23 am

I ran a quick test and the dialog opens to the correct path when I set the InitialDirectory property.

Does the specified folder exist? If not, it might default to another path.
David
SAPIEN Technologies, Inc.

User avatar
ocsscott
Posts: 47
Joined: Thu Dec 17, 2015 7:24 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by ocsscott » Thu Jun 27, 2019 12:43 pm

yes it does but I am displaying it and it looks like this

Microsoft.powershell.core\filesystem:\\The correct path

is the Microsoft.powershell.core\filesystem:\\ causing the issue?

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by davidc » Thu Jun 27, 2019 12:59 pm

Yes, that will cause the problem. You will have to trim that portion out.
David
SAPIEN Technologies, Inc.

User avatar
ocsscott
Posts: 47
Joined: Thu Dec 17, 2015 7:24 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by ocsscott » Thu Jun 27, 2019 1:03 pm

fyi I am getting the path with this code

$A = Get-Location
$global:LocalPath = $A.path.ToString()

to find where my exe is running then + "\subfolder" for the open location

is something wrong with that?

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by davidc » Thu Jun 27, 2019 1:12 pm

Get-Location will only give you the working directory which isn't necessarily the directory of the executable.

I recommend using the either:

Get-ScriptDirectory function (snippet: getscriptdirectory)
Or if you are using the latest version of PowerShell Studio 2019, the $PSScriptRoot variable.
David
SAPIEN Technologies, Inc.

User avatar
ocsscott
Posts: 47
Joined: Thu Dec 17, 2015 7:24 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by ocsscott » Thu Jun 27, 2019 1:54 pm

that worked much better and allowed the open dialog to work as expected.

Much thanks

Locked