Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Just starting out? Need help? Post your questions and find answers here.
Tawbie
User
User
Posts: 35
Joined: Fri Jul 10, 2020 2:36 am
Location: Australia

Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Post by Tawbie »

Suppose you wish to restrict the user of your application to save their work files in their standard windows documents folder and nowhere else. According to the PB manual "The DefaultFile$ is useful to initialize the requester in the right directory and with the right filename.", so you might code something like this:

Code: Select all

Dir$ = GetHomeDirectory() + "Documents\"                                  ; set default directory
DefaultFile$ = Dir$ + "MyFile.txt"                                        ; set default file
Pattern$ = "Text (*.txt)|*.txt"
Repeat
  File$ = SaveFileRequester("Save", DefaultFile$, Pattern$, 0)
    If File$ <> "" And GetPathPart(File$) <> Dir$ 
      MessageRequester("Error", "Please do not change directory.")
    EndIf
Until File$ = "" Or GetPathPart(File$) = Dir$                             ; exit if user cancels or directory unchanged
Now, if you run this code in the PB IDE, it works as expected. However, if you compile this code as a standalone exe and run it, then, if you change the directory, SaveFileRequester() does not return to the default "documents" directory as required. In fact, if you then re-run the exe, it launches with the changed directory.

Can anyone shed some light on this odd behavior? or is it a bug? or am I doing something wrong? Could Windows be somehow associating the last accessed directory with that exe file and overriding the default?

The same behavior occurs with OpenFileRequester().

Test environment: Windows 10 Pro, x64, PB5.73
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Post by BarryG »

Confirmed. Seems to be a bug. When I run the compiled exe now it even initially defaults to D: (my test drive) instead of DefaultFile$. It's like it's totally ignoring DefaultFile$ and using whatever I last selected, even after I quit the exe and restart it.
AZJIO
Addict
Addict
Posts: 2156
Joined: Sun May 14, 2017 1:48 am

Re: Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Post by AZJIO »

SetCurrentDirectory(Dir$) - it didn't help
I searched the MRU section in the registry until I found how to fix it.
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\
User avatar
mk-soft
Always Here
Always Here
Posts: 6231
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Post by mk-soft »

Is a Windows features (quirk)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
kenmo
Addict
Addict
Posts: 2034
Joined: Tue Dec 23, 2003 3:54 am

Re: Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Post by kenmo »

Yes, it's an annoying Windows "feature" for many years, not a PureBasic bug.

I looked into this a lot last year:
https://www.purebasic.fr/english/viewtopic.php?t=73909

And here is the simplest procedure/macro workaround I came up with:
https://www.purebasic.fr/english/viewto ... 67#p549667
Tawbie
User
User
Posts: 35
Joined: Fri Jul 10, 2020 2:36 am
Location: Australia

Re: Setting DefaultFile$ with SaveFileRequester() /OpenFileRequester() (Win10) - possible bug

Post by Tawbie »

Thanks kenmo and to all others for the feedback.
Post Reply