Page 1 of 1

$openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 6:01 am
by ocsscott6969
$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

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 7:19 am
by davidc
[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 7:23 am
by davidc
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.

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 12:43 pm
by ocsscott6969
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?

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 12:59 pm
by davidc
Yes, that will cause the problem. You will have to trim that portion out.

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 1:03 pm
by ocsscott6969
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?

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 1:12 pm
by davidc
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.

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Posted: Thu Jun 27, 2019 1:54 pm
by ocsscott6969
that worked much better and allowed the open dialog to work as expected.

Much thanks