TRUE:如果目的文件夹存在该文件,拷贝失败。 FALSE:如果目的文件夹存在该文件,则覆盖目的文件。 具体失败原因参考help The CopyFile function copies an existing file to a new file. BOOL CopyFile( LPCTSTR lpExistingFileName, // pointer to name of an existing file LPCTSTR lpNewFileName, // pointer to filename to copy to BOOL bFailIfExists // flag for operation if file exists ); ParameterslpExistingFileNamePoints to a null-terminated string that specifies the name of an existing file. lpNewFileNamePoints to a null-terminated string that specifies the name of the new file. bFailIfExistsSpecifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds. Return ValuesIf the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError. ResSecurity attributes for the existing file are not copied to the new file. File attributes (FILE_ATTRIBUTE_*) for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute. For further information on file attributes, see CreateFile.
uses ShellAPI, ComCtrls, FileCtrl, Outline, DirOutln;以下是拷贝过程,和Windows的拷贝一模一样 procedure CopyDirectoryTree(AHandle: THandle; const AFromDirectory, AToDirectory: String); var SHFileOpStruct:TSHFileOpStruct; FromDir:PChar; ToDir:PChar; begin GetMem(FromDir,Length(AFromDirectory)+2); try GetMem(ToDir,Length(AToDirectory)+2); try FillChar(FromDir^,Length(AFromDirectory)+2,0); FillChar(ToDir^,Length(AToDirectory)+2,0); StrCopy(FromDir,PChar(AFromDirectory)); StrCopy(ToDir,PChar(AToDirectory)); with SHFileOpStruct do begin Wnd :=AHandle; wFunc :=FO_COPY; pFrom :=FromDir; pTo :=ToDir; fFlags :=FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; fAnyOperationsAborted:=False; hNameMappings:=nil; lpszProgressTitle:=nil; end; if ShFileOperation(ShFileOpStruct)<>0 then RaiseLastWin32Error; finally FreeMem(ToDir,Length(AToDirectory)+2); end; finally FreeMem(FromDir,Length(AFromDirectory)+2); end; end;以下是删除过程,删除到回收站 procedure ToRecycle(AHandle:THandle; const ADirName: String); var SHFileOpStruct:TSHFileOpStruct; DirName:PChar; BufferSize:Cardinal; begin BufferSize:=Length(ADirName)+1+1; GetMem(DirName,BufferSize); try FillChar(DirName^,BufferSize,0); StrCopy(DirName,PChar(ADirName)); with SHFileOpStruct do begin Wnd:=AHandle; wFunc:=FO_DELETE; pFrom:=DirName; pTo:=nil; fFlags:=FOF_ALLOWUNDO; fAnyOperationsAborted:=False; hNameMappings:=nil; lpszProgressTitle:=nil; end; if SHFileOperation(SHFileOpStruct)<>0 then RaiselastWin32Error; finally FreeMem(DirName,BufferSize); end; end;
copyfile('C:\ABC.EXE','D:\abc.exe',true);
为什么我用false没用啊
FALSE:如果目的文件夹存在该文件,则覆盖目的文件。
具体失败原因参考help
The CopyFile function copies an existing file to a new file. BOOL CopyFile( LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
ParameterslpExistingFileNamePoints to a null-terminated string that specifies the name of an existing file. lpNewFileNamePoints to a null-terminated string that specifies the name of the new file. bFailIfExistsSpecifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds. Return ValuesIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError. ResSecurity attributes for the existing file are not copied to the new file.
File attributes (FILE_ATTRIBUTE_*) for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute. For further information on file attributes, see CreateFile.
ShellAPI, ComCtrls, FileCtrl, Outline, DirOutln;以下是拷贝过程,和Windows的拷贝一模一样
procedure CopyDirectoryTree(AHandle: THandle; const AFromDirectory,
AToDirectory: String);
var
SHFileOpStruct:TSHFileOpStruct;
FromDir:PChar;
ToDir:PChar;
begin
GetMem(FromDir,Length(AFromDirectory)+2);
try
GetMem(ToDir,Length(AToDirectory)+2);
try
FillChar(FromDir^,Length(AFromDirectory)+2,0);
FillChar(ToDir^,Length(AToDirectory)+2,0); StrCopy(FromDir,PChar(AFromDirectory));
StrCopy(ToDir,PChar(AToDirectory)); with SHFileOpStruct do
begin
Wnd :=AHandle;
wFunc :=FO_COPY;
pFrom :=FromDir;
pTo :=ToDir;
fFlags :=FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:=False;
hNameMappings:=nil;
lpszProgressTitle:=nil;
end;
if ShFileOperation(ShFileOpStruct)<>0 then
RaiseLastWin32Error;
finally
FreeMem(ToDir,Length(AToDirectory)+2);
end;
finally
FreeMem(FromDir,Length(AFromDirectory)+2);
end;
end;以下是删除过程,删除到回收站
procedure ToRecycle(AHandle:THandle; const ADirName: String);
var
SHFileOpStruct:TSHFileOpStruct;
DirName:PChar;
BufferSize:Cardinal;
begin
BufferSize:=Length(ADirName)+1+1; GetMem(DirName,BufferSize);
try
FillChar(DirName^,BufferSize,0);
StrCopy(DirName,PChar(ADirName)); with SHFileOpStruct do
begin
Wnd:=AHandle;
wFunc:=FO_DELETE;
pFrom:=DirName;
pTo:=nil;
fFlags:=FOF_ALLOWUNDO; fAnyOperationsAborted:=False;
hNameMappings:=nil;
lpszProgressTitle:=nil;
end; if SHFileOperation(SHFileOpStruct)<>0 then
RaiselastWin32Error;
finally
FreeMem(DirName,BufferSize);
end;
end;