Page 1 of 1

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

Posted: Sun Dec 19, 2021 8:36 am
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

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

Posted: Sun Dec 19, 2021 10:27 am
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.

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

Posted: Sun Dec 19, 2021 11:15 am
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\

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

Posted: Sun Dec 19, 2021 12:55 pm
by mk-soft
Is a Windows features (quirk)

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

Posted: Sun Dec 19, 2021 10:52 pm
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

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

Posted: Mon Dec 20, 2021 1:19 am
by Tawbie
Thanks kenmo and to all others for the feedback.