Force OpenFileRequester and SaveFileRequester start in Progr

Just starting out? Need help? Post your questions and find answers here.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Force OpenFileRequester and SaveFileRequester start in Progr

Post by VB6_to_PBx »

Force OpenFileRequester and SaveFileRequester to always start in my Program's Folder

example :
i startup my Program ,
i browse to a different Folder and Open up a File ,
then i use OpenFileRequester again, but its not starting in my Program's Folder,
instead its "stuck" in the previous Folder .

How can i force both OpenFileRequester and SaveFileRequester
to always start in my .EXE Program's Folder ??

Also here are other reasons for this request :

[1]= i have a User Registration / UnLock Code scheme in my Program
...potential problem is --> if the User travels to another Folder
and Opens an older version of my Program's Files ,
"BEFORE" he activates my Program with the UnLock Code ,
then decides to enter the UnLock Code at that point ,
the UnLock Code is put into the wrong Folder , and not in my Program's Folder .

[2]= in my Program , the User can just Drag/Drop
one of his old version Files into my new version Program
then He decides to activate my newer version Program ,
the UnLock Code again is put into the wrong Folder :(

Results of above [1] and [2] .. my Program never gets UnLocked , nor Activated :(


One more Question :
how can i force my PureBasic Program to change Drives ???
i can't find any info on this

Suppose my User changes Drives to a Jump Drive where his old Files are
to open them up with my new version software on his C:\ drive Folder
.... then decides to Activate my program ,
again the UnLock Code/Activation File gets put into the Jump Drive Folder
instead of where my new version Program Folder is ...
so that my Program never gets UnLocked , nor activated !

PS : i do not want to use the Window's Registry for this purpose .
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4951
Joined: Sun Apr 12, 2009 6:27 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by RASHAD »

Hi VB6_to_PBx
Adapt it for your needs

Code: Select all

; #OFN_ENABLESIZING       = $800000

Structure OPENFILENAME_EX Extends OPENFILENAME
  pvReserved.l
  dwReserved.l
  FlagsEx.i
EndStructure   
 
Procedure.s OpenFileRequester_Ex(Title$,Pattern$,Defdir$,Deffile$)
*Buffer = AllocateMemory(StringByteLength(Pattern$)+2)
For i = 0 To StringByteLength(Pattern$)     
     PokeB(*Buffer+i,PeekB(@Pattern$+i))
     If PeekB(@Pattern$+i) = 124
         PokeB(*Buffer+i,0)
      EndIf
Next
    of.OPENFILENAME_EX
    of\lStructSize = SizeOf(of)
    of\lpstrTitle = @Title$
    of\lpstrInitialDir = @Defdir$
    of\lpstrFilter = *Buffer
    of\nMaxFile = #MAX_PATH
    of\lpstrFile = @Deffile$
    of\lCustData = OpenYesNo
    of\Flags     = #OFN_EXPLORER | #OFN_LONGNAMES | #OFN_CREATEPROMPT | #OFN_ENABLEHOOK| #OFN_ENABLESIZING
    of\FlagsEx   = 0
    If GetOpenFileName_(of)
      File$ = PeekS(of\lpstrFile)
    EndIf
    ProcedureReturn File$
EndProcedure   
 

Title$   = "Please choose file to Open"
Pattern$ = "Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*"
Defdir$  = "c:\MR_Files"
Deffile$ = Space(250)

File$ = OpenFileRequester_Ex(Title$,Pattern$,Defdir$,Deffile$)
Debug File$

Egypt my love
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by BarryG »

VB6_to_PBx wrote:How can i force both OpenFileRequester and SaveFileRequester
to always start in my .EXE Program's Folder ??
Just use the "DefaultFile$" flag for both commands with your exe's dir:

Code: Select all

exedir$="c:\somewhere\on\the\pc\"
OpenFileRequester("Select file to open",exedir$,"",0)
SaveFileRequester("Select file to save",exedir$,"",0)
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by StarBootics »

BarryG wrote:
VB6_to_PBx wrote:How can i force both OpenFileRequester and SaveFileRequester
to always start in my .EXE Program's Folder ??
Just use the "DefaultFile$" flag for both commands with your exe's dir:

Code: Select all

exedir$="c:\somewhere\on\the\pc\"
OpenFileRequester("Select file to open",exedir$,"",0)
SaveFileRequester("Select file to save",exedir$,"",0)
You can get the exe path with :

Code: Select all

Path.s = GetPathPart(ProgramFilename())
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by VB6_to_PBx »

BarryG
Just use the "DefaultFile$" flag for both commands with your exe's dir:
StarBootics
Path.s = GetPathPart(ProgramFilename())
RASHAD , BarryG , StarBootics
sorry for this late Reply , i've been testing on Win7 + Win8.0 + Win10

i've been using
Path.s = GetPathPart(ProgramFilename())
from the beginning on my Win10 Computer
it works ! , but twice , after WIN10 Computer has been on 6 or 8 hours ,
it does not work anymore ... if i restart WIN10 Computer , it works perfect again ... what's going on ??

Also on WIN10 , if i startup my Program in its Folder on C:/ drive , and Browse to a Folder on a Jump Drive or External Drive
it also works perfect , but if i start this same Program from the G/Drive or some Jump Drive
and Browse around to other Drive Folders , it fails :(

so i gave up for awhile , and just created a separate Registration / UnLock Code .EXE
that never Opens/Saves or uses Windows File Dialogs , and that works perfect everytime
no matter what Drive or Folder i start in .

Path.s = GetPathPart(ProgramFilename())
on WIN7 and WIN8.0 , it has never worked :(
... meaning , it never stays or starts up everytime in my Program's Folder ,
Windows File Dialog gets stuck again in the previous Folder it went to .

RASHAD , i'll test out your Code later ,
i need to get back to programming other parts of my Program,
then i'll come back and try and conquer this thing again .
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by BarryG »

Deleted because I misunderstood the problem. Sorry.
Last edited by BarryG on Mon Oct 28, 2019 11:22 am, edited 1 time in total.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by VB6_to_PBx »

RASHAD ,
i just tested your Code on WIN7 ,WIN8.0, WIN10

on WIN10 it works perfect as long as i startup from my C:\ drive Program Folder
i can go to Jump Drive and External Drive it works !

Also if i place this same exact .EXE into my Jump Drive
i can browse and ope on other Folders , and it always starts up in my original Program Folder !

However , if i place this same exact .EXE into my Western Digital external drive
it 1st starts up correctly in my Program's Folder
but if i browse to other Folders or Drives .. again it stuck opening in those Folders , never back to my original Program Folder :(

if i close , and restart my Program with your Code .. same problem= its still stuck trying to open Files in previous Folder
it never reverts back to my original Folder :(

on my Jump Drive is just cheap Cruiser32G Jump Drive ... it works with your Code on WIN 10 computer
on my Western Digital 4T external Drive .. your Code never works :(

on my WIN7 and WIN8.0 , with either Cruiser32G Jump Drive or Western Digital 4T external Drive , your Code does not work like on WIN 10 :(
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by Marc56us »

VB6_to_PBx wrote:Force OpenFileRequester and SaveFileRequester to always start in my Program's Folder

Code: Select all

SetCurrentDirectory(GetPathPart(ProgramFilename()))

OpenFileRequester("", GetCurrentDirectory(), "", 0)

SaveFileRequester("", GetCurrentDirectory(), "", 0)
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by VB6_to_PBx »

BarryG wrote:
VB6_to_PBx wrote:Path.s = GetPathPart(ProgramFilename())
on WIN7 and WIN8.0 , it has never worked :(
Trust me: it does work. I've used this exact code in my apps from Win XP to Win 10 and it works. Even when running my apps from a USB stick. So I'd say either your variable for it ("Path.s") isn't global, or gets changed somewhere by mistake. Without seeing your code we can't really diagnose the issue, but it does work. Like I said, I've used it this way literally forever.

Maybe you're falling into the old trap of that code getting the temp executable path when running your app from the PureBasic IDE, instead of your app's actual path where the exe is built? You need to account for that. Here's how I do it:

Code: Select all

Global devdir$="C:\MyApp\"
Global devexe$="MyApp.exe"

Global appname$=ProgramFilename() ; Assumes app was run from the built exe.
Global appdir$=GetPathPart(appname$) ; Assumes app was run from the built exe.

CompilerIf #PB_Compiler_Debugger ; But it wasn't! We're in the IDE, so...
  appname$=devdir$+devexe$ ; ...set them both to be...
  appdir$=devdir$ ; ...where the built exe will be.
CompilerEndIf
So whether my app is run from the IDE or from the built exe, it always correctly sets appname$ and appdir$ to where the exe will be (or is) built. Thus meaning the app can always find its required settings and files and so on.
Without seeing your code we can't really diagnose the issue,
Yes , i should Posted my Code example first off :oops:

here it is :
i created an .EXE .... so i'm testing / running testing my Code in that .EXE program

i have this Declared in a Module

Code: Select all

Global.s AppPath = GetCurrentDirectory()
Global.s ProgramPath = GetPathPart(ProgramFilename())

Global.s FileOpenTempName , FileOpenName , FileOpenNameTitle , PathPartOpenName , FilePartOpenName , FileExtensionOpenName
Global.s FileSaveTempName , FileSaveName , FileSaveNameTitle , PathPartSaveName , FilePartSaveName , FileExtensionSaveName
and this is also in the same Module

Code: Select all

; Declare FileOpen(State.i)
Procedure FileOpen(State.i)
    Select State
    Case 0 : Debug "FileOpen State 0 = " + State
    Case 1 : Debug "FileOpen State 1 = " + State
    Case 2 : MessageRequester(Space(3) + "Information :  Recent File List" + Space(3), Space(1) + "Open Recent File List request", 0) : ProcedureReturn
    EndSelect

    Protected.s PatternOpen
    PatternOpen + "Text Files (*.txt)|*.txt|"  ; (index = 0)
    PatternOpen + "All Files  (*.*)|*.*|"      ; (index = 1)
    PatternOpenIndex.i = 0  

    Debug "you are at Procedure FileOpen(State.i)"

    ;Protected T.s
    ;~~~~~~~~~~ Result = MessageRequester(Title$, Text$ [, Flags])
    ;~~~~~~~~~~ #PB_MessageRequester_Ok          ;~~~~~~~~~~ 'OK' only one button (Standard)
    ;~~~~~~~~~~ #PB_MessageRequester_YesNo       ;~~~~~~~~~~ 'YES' or 'NO' buttons
    ;~~~~~~~~~~ #PB_MessageRequester_YesNoCancel ;~~~~~~~~~~ 'YES', 'NO' and 'CANCEL' buttons   2= CANCEL  6= YES  7= NO
    Title$ = "  Warning  :  File's  Data  was  changed ! "
    Text$ = "  this File's Data has been changed ...."  + #CRLF$ + #CRLF$ + "  do you want to Save the new changes you made ?" + #CRLF$

    If DirtyC > 0  Or DirtyS > 0 ;<<--- ComboBoxGadget choice or StringGadget input text , was changed by User
					    Debug "DirtyC = " + DirtyC
					    Debug "DirtyS = " + DirtyS

         Result = MessageRequester(Title$, Text$, #PB_MessageRequester_YesNoCancel)  ;<<---  2= CANCEL   6= YES   7= NO 
         Select Result
         Case 2 : ProcedureReturn               ;~~~~~~~~~~ 2= CANCEL 
         Case 6 : FileSave(1): ProcedureReturn  ;~~~~~~~~~~ 6= YES ... Save new changes to File
         Case 7                                 ;~~~~~~~~~~ 7= NO  ... Do not Save new changes ... continue to Open new File
         EndSelect
    EndIf

    FileOpenTempName = OpenFileRequester("Open File : " + Space(3) + FileOpenName, ProgramPath + FilePartOpenName, PatternOpen, PatternOpenIndex)
    ;FileOpenTempName = OpenFileRequester("Open File : " + Space(3) + FileOpenName, AppPath + FilePartOpenName, PatternOpen, PatternOpenIndex)

    If Len(FileOpenTempName) > 0
         FileOpenName = FileOpenTempName 
         FileSaveName = FileOpenName
         PathPartOpenName = GetPathPart(FileOpenName) : Debug "PathPartOpenName = " + PathPartOpenName
         FilePartOpenName = GetFilePart(FileOpenName) : Debug "FilePartOpenName = " + FilePartOpenName
         FileExtensionOpenName = GetExtensionPart(FileOpenName) : Debug "FileExtensionOpenName = " + FileExtensionOpenName ;
         SetWindowTitle(1, PathPartOpenName + FilePartOpenName)
    Else
         MessageRequester(Space(3) + "Information :  No File Opened" + Space(3), " Open File request was canceled ", 0)
         ProcedureReturn          
    EndIf


EndProcedure
and i'm opening the Folder with this by a ButtonImageGadget in my Program and also by Menu choice

Code: Select all

Case 4741 ;-[ 4741 ] Open Folder
     FileOpen(0)
Menu choice

Code: Select all

              Case 1 ;-[ 1 ] Open File
                   FileOpen(0)

the

Code: Select all

SetWindowTitle(1, PathPartOpenName + FilePartOpenName) 
also shows where my EXE 1st starts up at


Edit :
original Thread Title : Force OpenFileRequester and SaveFileRequester to always start in my Program's Folder

i should made it read : Force OpenFileRequester and SaveFileRequester to always stay in my Program's Folder
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by Marc56us »

VB6_to_PBx wrote: Edit :
original Thread Title : Force OpenFileRequester and SaveFileRequester to always start in my Program's Folder
i should made it read : Force OpenFileRequester and SaveFileRequester to always stay in my Program's Folder
Use my example above
Without path, FileRequester use current directory, so use SetCurrentDirectory() before Open/Save with PB Requester.

This is not PB function or bug, this is how the operating system works (so API)

(Tested on Windows 10: always work)
:wink:
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by VB6_to_PBx »

Marc56us wrote:
VB6_to_PBx wrote: Edit :
original Thread Title : Force OpenFileRequester and SaveFileRequester to always start in my Program's Folder
i should made it read : Force OpenFileRequester and SaveFileRequester to always stay in my Program's Folder
Use my example above
Without path, FileRequester use current directory, so use SetCurrentDirectory() before Open/Save with PB Requester.

This is not PB function or bug, this is how the operating system works (so API)

(Tested on Windows 10: always work)
:wink:
hi Marc56us ,
i just tested your Code on my WIN10 starting from my C:\ drive Folder and it works
but if i paste this same EXE to Western Digital external drive
and run it , it does start up in original Folder on the Western Digital external drive,
but if i browse to a different Folder , its now stuck opening in that Folder , no matter if i end and restart my EXE

same old Problems as before :(

here's what i intended to make it work like :

Procedure FileOpen(State.i)
Select State
Case 0 : ; continue to Open the File normally ... wherever Folder/Drive it might be located
Case 1 : ; use ProgramParameter() to let User Open file thru associated file
Case 2 : ; User chose Menu # 3 which open Recent Files History of last 100 opened files in my Program in a ListViewGadget with Editing Features , etc
Case 3 : ; Let the User Force Windows to Open my simulation file in my Program's original Folder ... no matter what Folder or Drive was previously accessed/opened
EndSelect

Case 3 is what i need to solve , if possible for all circumstances + all Window versions ( ie, WIN-XP to WIN10 )

and i need a solution for my Registration/UnLock part of code to always startup in my Programs original Folder under all circumstances + all Window versions

the only 100% percent working solution so far is me creating the separate Registration/UnLock EXE i made
that never uses Windows File Dialogs ... just saves the UnLock Encrypted Code file on the User's Folder where my main Program is located

i guess i'll need to stick with that ???
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by BarryG »

Deleted because I misunderstood what was being asked.
Last edited by BarryG on Mon Oct 28, 2019 11:21 am, edited 1 time in total.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by Marc56us »

Lost 8)

Open : OK
Save a file in another folder : OK
Open : KO (still in last folder where save)
Save: KO (last folder too)
Close application, open again: KO Last folder where last save

Next candidate ? :P
BarryG
Addict
Addict
Posts: 4160
Joined: Thu Apr 18, 2019 8:17 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by BarryG »

So it's a bug with the DefaultFile$ flag of both requesters then, because the default file ISN'T being used.

MSDN says: "To prevent the dialog box from changing your current directory, set the OFN_NOCHANGEDIR flag."
Source: https://docs.microsoft.com/en-us/window ... alog-boxes

Sounds like the open/save dialogs of PureBasic aren't doing that? Maybe this is a needed feature request?

[Edit] Rashad's code seems to work perfectly... as always. Hehe.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4951
Joined: Sun Apr 12, 2009 6:27 am

Re: Force OpenFileRequester and SaveFileRequester start in P

Post by RASHAD »

Hi VB6_to_PBx
Running your code from external Drive(maybe USB) needs to take care about the OS mapping letter to your external drive.
Which is for sure different per OS
If it is H:\ in Windows 7 for example that does not mean it will be H:\ again in Windows 8 and so on
You must know your drive assigning letter before past it to your code
Egypt my love
Post Reply