It is currently Wed Nov 13, 2019 10:39 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: [Solved - OS fault] File Requesters open in previous path
PostPosted: Thu Oct 31, 2019 1:34 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1671
Spin off this thread: viewtopic.php?f=13&t=73886

This is a minor "bug" behavior I have noticed for a while but never investigated.
The File Requesters open in the last selected directory, even if you give a specific directory argument.

It's either PureBasic or Windows, caching the directory in some way? Can it be overridden?

Code:
CompilerIf (#PB_Compiler_Debugger)
  MessageRequester("Instructions",
      "1. Compile this to an EXE (Desktop/test.exe or similar)" + #LF$ +
      "2. Run it" + #LF$ +
      "3. When the first requester opens, choose a file in a different folder" + #LF$ +
      "4. The second requester opens in that folder (bug)" + #LF$ +
      "5. Run the program again, and it opens in that changed folder (bug)" + #LF$ + #LF$ +
      "Some sort of Windows caching issue ??" )
CompilerElse

  ;CurrentDir.s = GetCurrentDirectory()
  ;MessageRequester("Current Dir", CurrentDir)
 
  ProgramDir.s = GetPathPart(ProgramFilename())
  MessageRequester("Program Dir", ProgramDir)
 
  OpenFileRequester("Title", ProgramDir, "All Files|*.*", 0)
  OpenFileRequester("Title", ProgramDir, "All Files|*.*", 0)

CompilerEndIf

_________________
On GitHub: PB Includes - IDE Tools - Color Themes


Last edited by kenmo on Tue Nov 05, 2019 8:45 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: File Requesters open in previous path
PostPosted: Thu Oct 31, 2019 1:47 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1671
This seems to be why. Can it be overridden?
https://docs.microsoft.com/en-us/window ... nfilenamea

Quote:
lpstrInitialDir

Type: LPCTSTR

The initial directory. The algorithm for selecting the initial directory varies on different platforms.

Windows 7:

If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.
Otherwise, if lpstrFile contains a path, that path is the initial directory.
Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
Otherwise, the initial directory is the personal files directory of the current user.
Otherwise, the initial directory is the Desktop folder.

Windows 2000/XP/Vista:

If lpstrFile contains a path, that path is the initial directory.
Otherwise, lpstrInitialDir specifies the initial directory.
Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded.
If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
Otherwise, the initial directory is the personal files directory of the current user.
Otherwise, the initial directory is the Desktop folder.


I guess it's been a Windows complaint for 8+ years :)
https://social.msdn.microsoft.com/Forum ... w-to-avoid

_________________
On GitHub: PB Includes - IDE Tools - Color Themes


Top
 Profile  
Reply with quote  
 Post subject: Re: File Requesters open in previous path
PostPosted: Thu Oct 31, 2019 7:20 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1671
Sorry to reply to my own question, but I figured out a workaround that seems to work on Windows 10.
Just 2 procedures + 1 call, before you open any requesters.

Windows 7+ remembers the first DefaultDir you call. If you call that DefaultDir again, Windows ignores it and opens in the last selected path.

Apparently this was an attempt to solve lazy programs which don't track the user's selected path, when the user expects it to. But it broke other programs' intended behavior.

So a workaround is... first open DefaultDir to a temporary folder you'll likely never call again. Then future DefaultDir will work as expected.
Preferably, open DefaultDir as hidden as possible, in this workaround it closes it as soon as possible.


Code:
;-
;- -----------------------------
;- WORKAROUND START
;-

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
Procedure _PrepareFileRequester(*OFN.OPENFILENAME)
  OpenFileRequester(PeekS(*OFN\lpstrTitle), PeekS(*OFN\lpstrInitialDir), "", 0)
EndProcedure

Procedure PrepareFileRequester()
  Static Prepared.i = #False
  If (Not Prepared)
    Prepared = #True
    If (OSVersion() >= #PB_OS_Windows_7)
      Protected Title.s = " "
      While (FindWindow_(#Null, @Title))
        Title + " "
      Wend
      Protected TempDir.s = GetTemporaryDirectory() + "." + Str(Date())
      CreateDirectory(TempDir)
      Protected OFN.OPENFILENAME
      OFN\lpstrTitle      = @Title
      OFN\lpstrInitialDir = @TempDir
      Protected *Thread = CreateThread(@_PrepareFileRequester(), @OFN)
      Protected *Window
      Repeat : Delay(0) : *Window = FindWindow_(#Null, @Title) : Until (*Window)
      SendMessage_(*Window, #WM_CLOSE, 0, 0)
      Repeat : Delay(0) : Until (Not IsThread(*Thread))
      DeleteDirectory(TempDir, "")
    EndIf
  EndIf
EndProcedure

CompilerElse
Macro PrepareFileRequester()
  ;
EndMacro
CompilerEndIf

;-
;- WORKAROUND END
;- -----------------------------


;-
;-

CompilerIf (#PB_Compiler_Debugger)
  MessageRequester("Instructions",
      "1. Compile this to an EXE (Desktop/test.exe or similar)" + #LF$ +
      "2. Run it" + #LF$ +
      "3. When the first requester opens, choose a file in a different folder" + #LF$ +
      "4. The second requester opens in that folder (bug)" + #LF$ +
      "5. Run the program again, and it opens in that changed folder (bug)" + #LF$ + #LF$ +
      "Some sort of Windows caching issue ??" )
CompilerElse
 
  ProgramDir.s = GetPathPart(ProgramFilename())
  MessageRequester("Program Dir", ProgramDir)
 
  PrepareFileRequester() ;- ADD THIS ONE LINE
 
  OpenFileRequester("Title", ProgramDir, "All Files|*.*", 0)
  SaveFileRequester("Title", ProgramDir, "All Files|*.*", 0)

CompilerEndIf
;-

_________________
On GitHub: PB Includes - IDE Tools - Color Themes


Top
 Profile  
Reply with quote  
 Post subject: Re: File Requesters open in previous path
PostPosted: Sun Nov 03, 2019 3:50 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Mar 09, 2006 9:24 am
Posts: 263
Location: S. Kor
viewtopic.php?f=12&t=13682

That code seems to work properly.


Top
 Profile  
Reply with quote  
 Post subject: Re: File Requesters open in previous path
PostPosted: Sun Nov 03, 2019 11:23 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1671
Not for me (tested on Win 7 just now).
It uses the OpenFileName_() API, which has the issue, exactly what is discussed in that MSDN link.
https://social.msdn.microsoft.com/Forum ... w-to-avoid

Maybe you misunderstand the (subtle) issue.
Take Justin's code and replace the bottom with this:

Code:
;- #TEST
; compile an EXE (with a unique name) and run it
; the first dialog opens in Desktop (correct)
; the second dialog ignores Desktop and opens in whatever folder you last selected
GetOpenFileName(0, "", GetUserDirectory(#PB_Directory_Desktop), "All files (*.*)|*.*|MP3 (*.mp3)|*.mp3", 1, "", "", #Null, @iFilter, @fileTitle$, #OFN_FILEMUSTEXIST)
GetOpenFileName(0, "", GetUserDirectory(#PB_Directory_Desktop), "All files (*.*)|*.*|MP3 (*.mp3)|*.mp3", 1, "", "", #Null, @iFilter, @fileTitle$, #OFN_FILEMUSTEXIST)



This answer from that link is interesting, maybe I'll experiment with this, and if it works, suggest it to the PB team as a fix.
Quote:
You should be able to put the full path in the lpstrFile field of the OPENFILENAME struct (and leave lpstrInitialDir empty). This is generally what is used in the "Save As" scenario for instance, where the dialog starts in the same folder as the item you are "save as"ing exists. You'll probably need to supply a filename too though (which will appear in the filename box).

EDIT 2019-11-05 This seems to work (specify a lpstrFile and it will always open in that folder, the OS won't ignore your specification) BUT it seems you MUST specify a file name (even ".") which is a small annoyance, you can't open with an empty name field.

_________________
On GitHub: PB Includes - IDE Tools - Color Themes


Last edited by kenmo on Tue Nov 05, 2019 8:48 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: File Requesters open in previous path
PostPosted: Mon Nov 04, 2019 12:05 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Mar 09, 2006 9:24 am
Posts: 263
Location: S. Kor
OK, you are right. The compiled exe doesn't work.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye