I'm aware that the directory where the current executable is located can be found using Application.Exename.
But when the application was started using a shortcut in another directory? Can I find the address of THAT directory, where the shortcut is, as I'd like to create some files there?
Using XE2. Many thanks.
You can do that using GetStartupInfo with the STARTF_TITLEISLINKNAME flag:
const
STARTF_TITLEISLINKNAME = $800;
function GetShortcutName(out LinkName: string): Boolean;
var
si: TStartupInfo;
begin
Result := False;
FillChar(si, SizeOf(TStartupInfo), 0);
GetStartupInfo(si);
if (si.dwFlags and STARTF_TITLEISLINKNAME) <> 0 then
begin
Result := True;
LinkName := si.lpTitle;
end;
end;
Test code (tested on Win7 64 with XE8 and Delphi 10 Seattle - not tested on Win8 or 10):
program GetShortCutTest;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
Windows;
var
sLink: string;
begin
if GetShortcutName(sLink) then
WriteLn('Shortcut: ' + sLink)
else
WriteLn('Not run from shortcut.');
ReadLn;
end.
You can test it by running the test app (which will show 'Not run from shortcut.'), and then creating a shortcut to the test app and executing that shortcut (which then shows 'Shortcut: ' and the name of the .lnk file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With