$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.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ocsscott6969
Posts: 48
Last visit: Tue Dec 05, 2023 10:04 am

$openfiledialog1.InitialDirectory not opening in correct folder

Post 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
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post 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.
David
SAPIEN Technologies, Inc.
User avatar
ocsscott6969
Posts: 48
Last visit: Tue Dec 05, 2023 10:04 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post 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?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by davidc »

Yes, that will cause the problem. You will have to trim that portion out.
David
SAPIEN Technologies, Inc.
User avatar
ocsscott6969
Posts: 48
Last visit: Tue Dec 05, 2023 10:04 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post 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?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post 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.
David
SAPIEN Technologies, Inc.
User avatar
ocsscott6969
Posts: 48
Last visit: Tue Dec 05, 2023 10:04 am

Re: $openfiledialog1.InitialDirectory not opening in correct folder

Post by ocsscott6969 »

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

Much thanks
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked