Inno Setup 不支持原生创建硬链接。
我不会将mklink 视为外部应用程序。它是一个内置的 Windows 工具。因此,如果您不需要支持 Windows XP,您可以放心地依赖它。或者,如果 mklink 不可用,您可以退回到定期安装 DLL。
或使用Code 部分中的CreateHardLink function。
#define MyApp "MyApp"
#define UninstallDll "uninstall.dll"
[Files]
Source: "{#UninstallDll}"; DestDir: "{cf}\{#MyApp}"; \
Flags: ignoreversion uninsneveruninstall
[Code]
function CreateHardLink(lpFileName, lpExistingFileName: string;
lpSecurityAttributes: Integer): Boolean;
external 'CreateHardLinkW@kernel32.dll stdcall';
procedure CurStepChanged(CurStep: TSetupStep);
var
ExistingFile, NewFile: string;
begin
if CurStep = ssPostInstall then
begin
ExistingFile := ExpandConstant('{cf}\{#MyApp}\{#UninstallDll}');
NewFile := ExpandConstant('{app}\{#UninstallDll}');
if CreateHardLink(NewFile, ExistingFile, 0) then
begin
Log('Hardlink created');
end
else
if FileCopy(ExistingFile, NewFile, False) then
begin
{ FAT file system? }
Log('Hardlink could not be created, file copied instead');
end
else
begin
MsgBox('Cannot install {#UninstallDll}', mbError, MB_OK);
end;
end;
end;
(在 Unicode version of Inno Setup 上测试 - Inno Setup 6 的唯一版本)
并且卸载时不要忘记删除文件:
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
if CurUninstallStep = usUninstall then
begin
if DeleteFile(ExpandConstant('{app}\{#UninstallDll}')) then
begin
Log('File deleted');
end
else
begin
Log('Cannot delete file');
end;
end;
end;
您当然也可以使用[UninstallDelete] 条目。我只是喜欢使用与安装它相同的技术来卸载该文件。
您的问题标题是“使用 Inno Setup 创建硬链接”。
CreateHardLink 创建一个硬链接。硬链接是对相同内容的另一个引用。基本上,硬链接与原始文件无法区分(即使原始文件实际上也是 hardlink)。原始文件和硬链接都只是对相同内容的引用。如果您删除原始文件(或新的硬链接),您实际上只删除了对内容的一个引用。内容仍然保留。内容仅与最后一次引用一起被删除。硬链接不占用磁盘上的额外空间(内容只存储一次)。
详情请见Hard link article on Wikipedia。
虽然mklink 默认创建一个符号链接(又名符号链接)。符号链接就像一个快捷方式,它是对原始文件(而不是内容)的引用。它本身就是一个文件,其中包含目标文件的路径。符号链接有自己的大小(由对目标文件的引用占用)。如果删除原始文件,符号链接仍然存在(因为原始文件中没有对符号链接的引用),但变得无效(内容消失了)。同样,它类似于快捷方式。
详情见Symbolic link article on Wikipedia。
如果添加/H 开关,您可以使用mklink 创建硬链接:
/H 创建硬链接而不是符号链接。
如果你想创建符号链接而不是硬链接,这是一个不同的问题(虽然答案很简单,请使用CreateSymbolicLink function)。尽管如此,请注意硬链接不会占用磁盘上的额外空间,这似乎是您关心的问题。所以我相信你应该继续使用CreateHardLink函数。