我正在开发一个安装程序,需要在安装之前创建目录的备份。我实现的方法纯粹是将当前目录中的所有文件复制到新目录,然后我可以随意覆盖旧目录中的文件(我的安装程序)。
但是,我收到一条提示,指示文件复制失败
,但我就是不明白为什么它不起作用。我的错误消息打印了正确的目录\文件名,我可以验证它们是否存在并且未在任何外部程序中打开。
下面是代码,取自(并稍作修改):http://blogs.candoerz.com/question/139833/inno-setup-copy-folder-subfolders-and-files-recursively-in-code-section.aspx
function DirectoryCopy(SourcePath, DestPath: string): boolean;
var
FindRec: TFindRec;
SourceFilePath: string;
DestFilePath: string;
begin
if FindFirst(SourcePath + '\*', FindRec) then begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then begin
SourceFilePath := SourcePath + '\' + FindRec.Name;
DestFilePath := DestPath + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then begin
if FileCopy(SourceFilePath, DestFilePath, False) then begin
Result := True;
MsgBox('Copy Worked!', mbInformation, MB_OK);
end else begin
Result := False;
MsgBox('Copy Failed!'+SourceFilePath, mbInformation, MB_OK);
end;
end else begin
if CreateDir(DestFilePath) then begin
Result := True;
MsgBox('Created Dir!', mbInformation, MB_OK);
DirectoryCopy(SourceFilePath, DestFilePath);
end else begin
Result := False;
MsgBox('Failed to create Dir!', mbInformation, MB_OK);
end;
end;
end;
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end else begin
Result := False;
MsgBox('Failed to List!', mbInformation, MB_OK);
end;
end;
最佳答案
我怀疑您尝试复制到的目录不存在。您需要首先使用 CreateDir
或 ForceDirectories
创建目录。文件函数(包括 Martin 的 DirectoryCopy
函数,它使用这些内部函数)要求目录存在,否则将会失败。他们不会自动为您创建路径。遗憾的是,这在任何地方都没有记录(我可以找到,尽管有人可能能够纠正我),因为它也让我困惑了一段时间。
Martin 的 DirectoryCopy
函数的原始链接可用 here .
关于inno-setup - Inno安装程序文件复制失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535942/