Delphi:启动应用程序的快捷方式在哪里? [英] Delphi: Where is the shortcut that started the application?
问题描述
我知道可以使用Application.Exename找到当前可执行文件所在的目录。
I'm aware that the directory where the current executable is located can be found using Application.Exename.
但是当应用程序使用另一个快捷方式启动时目录?我想在其中创建一些文件时找到快捷方式所在的THAT目录的地址吗?
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?
使用XE2。非常感谢。
Using XE2. Many thanks.
推荐答案
您可以使用 GetStartupInfo 与 STARTF_TITLEISLINKNAME
标志:
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;
测试代码(在带有XE8和Delphi 10 Seattle的Win7 64上测试-未在Win8或10上测试) :
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.
您可以通过运行测试应用程序进行测试(显示为不是从快捷方式运行)。 ,然后创建测试应用程序的快捷方式并执行该快捷方式(然后显示 Shortcut:和.lnk文件的名称)。
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).
这篇关于Delphi:启动应用程序的快捷方式在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!