Mega Code Archive

 
Categories / Delphi / Examples
 

Dirpaths

the quick way to get the path to the application exe file: var exeFilePath: String; begin exeFilePath := ExtractFilePath(Application.ExeName); end; *********************************** Useful info to do with file paths: IF THE USER OPENS OR SAVES FILES IN A DIFFERENT DIRECTORY TO THE ONE WHERE THE .EXE FILE IS, THEN AS FAR AS THE PROGRAM IS CONCERNED THE 'CURRENT DIRECTORY' WILL THEREFORE HAVE CHANGED- so, to avoid 'file not found' errors where we subsequently try and use a simple filename without the extension, we need to keep track of the 'startup' directory for the program: code below illustrates this: const helpFileName = 'MyHelp.hlp'; procedure TSrchForm.HelpButtonClick(Sender: TObject); {run the Helpfile for the application -as it stands, the Helpfile must be in the STARTUP directory along with the associated contents file, so both Search.hlp and Search.cnt must be present...} var exeFileName, pathToExe, fileName: String; begin {because changing the directory in the file List box changes the overall 'current directory' such that the program won't find the Help file if we're not careful, we need to keep track of the path to the .exe (and therefore the .hlp) WHEN THE PROGRAM FIRST RUNS...} exeFileName := Application.ExeName; pathToExe := ExtractFilePath(exeFileName); fileName := pathToExe + helpFileName; Application.HelpFile := fileName; Application.HelpCommand(HELP_CONTENTS, 0); end; See also the InitialDir property of Save and Open Dialog boxes... *************************************OR*********************************** main form object attributes: private var helpFileName: String; dictFileName: String; initialDir: String; end; procedure TCWForm.FormCreate(Sender: TObject); begin helpFileName := ExpandFileName('Search.hlp'); dictFileName := ExpandFileName('dict.txt'); initialDir := ExtractFilePath(helpFileName); end; procedure AnyOldProcedure; var fullDictFileName: String; begin fullDictFileName := initialDir + dictFileName; end; procedure TCWForm.HelpButtonClick(Sender: TObject); begin Application.HelpFile := helpFileName; Application.HelpCommand(HELP_CONTENTS, 0); end;