[Solved - OS fault] File Requesters open in previous path

Windows specific forum
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [Solved - OS fault] File Requesters open in previous pat

Post by kenmo »

Anyway, I had a little revelation today, and found a much nicer fix!!!

Instead of create a temporary folder, multi-thread, open and close a dummy requester (ugly hack!)...

it's enough to just slightly modify the path string, the first time you open a requester.
For example, change C:\Users\Me\Downloads\ to C:\Users\Me\Downloads\<timestamp>\..\
That is enough to prevent Windows from seeing an identical string and changing your path --- plus, it will open in the folder you really want without having to create that <timestamp> dummy subfolder!

Code: Select all

; Adapt this however you want
Procedure.s _OpenFileRequester(Title.s, DefaultFile.s, Pattern.s, PatternPosition.i, Flags.i = 0)
  
  ; Workaround for Windows 7+ behavior: slightly manipulate path the first time you open a requester
  Static HasRun.i = #False
  If (Not HasRun)
    HasRun = #True
    If (DefaultFile = "")
      DefaultFile = GetCurrentDirectory()
    EndIf
    DefaultFile = GetPathPart(DefaultFile) + Str(Date()) + "\..\" + GetFilePart(DefaultFile)
    MessageRequester("", "!!! Modified the first call's path to:" + #LF$ + DefaultFile)
  EndIf
  
  ProcedureReturn OpenFileRequester(Title, DefaultFile, Pattern, PatternPosition, Flags)
EndProcedure

; You can use a macro to override the native function
Macro OpenFileRequester(Title, DefaultFile, Pattern, PatternPosition, Flags = 0)
  _OpenFileRequester(Title, DefaultFile, Pattern, PatternPosition, Flags)
EndMacro

CompilerIf (#PB_Compiler_Debugger)
  MessageRequester("Instructions",
      "1. Compile this to an EXE (Desktop\SomethingUnique.exe)" + #LF$ +
      "2. Run it" + #LF$ +
      "3. When the first requester opens, choose a file in a DIFFERENT folder" + #LF$ +
      "4. The second requester SHOULD open in the original folder (bug fixed!)" + #LF$ +
      "5. Run the program again - it SHOULD open in the original folder too")
CompilerElse
  
  ProgramDir.s = GetUserDirectory(#PB_Directory_Downloads)
  MessageRequester("", "Should open in:" + #LF$ + ProgramDir)
 
  OpenFileRequester("Title", ProgramDir, "All Files|*.*", 0)
  OpenFileRequester("Title", ProgramDir, "All Files|*.*", 0)

CompilerEndIf
JHPJHP I see we are writing at the same time... I will try your double backslash code on Win 7
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

[Solved - OS fault] File Requesters open in previous path

Post by JHPJHP »

Hi kenmo,
kenmo wrote:I will try your double backslash code on Win 7
Yeah, no worries.

If the code you posted works on Windows 7, then I'm fairly confident the code I’m using in my projects (see my previous post) should work as well.
- we are both working from the same principle, subsequent calls to OpenFileRequester have a slightly different DefaultFile path from the first call

Good to know there is still some consistency across Windows versions.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [Solved - OS fault] File Requesters open in previous pat

Post by Little John »

@JHPJHP:
Your second code solves the issue on my system.
kenmo wrote:it's enough to just slightly modify the path string, the first time you open a requester.
For example, change C:\Users\Me\Downloads\ to C:\Users\Me\Downloads\<timestamp>\..\
This works as expected here, too.

Thanks to both of you!
Post Reply