[英]How to correctly use IFileOperation in Delphi to delete the files in a folder
[英]using IFileOperation in delphi 7
我想使用IFileOperation CopyItem
将文件从一个目录复制到另一个目录,在delphi 7中是否有一个简单的示例?
我找到了MSDN文档 ,其中包括一个示例。 这是转换为Delphi的示例:
uses ActiveX, ComObj, ShlObj;
function TForm1.CopyItem(const aSrcItem, aDest, aNewName: string): HRESULT;
const
CLSID_FileOp: TGUID = '{3ad05575-8857-4850-9277-11b85bdb8e09}';
var
lFileOperation: IFileOperation;
psiFrom: IShellItem;
psiTo: IShellItem;
begin
//
// Initialize COM as STA.
//
Result := CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);
if Succeeded(Result) then
begin
//
// Create the IFileOperation interface
//
Result := CoCreateInstance(CLSID_FileOp, nil, CLSCTX_ALL, IFileOperation,
lFileOperation);
if Succeeded(Result) then
begin
//
// Set the operation flags. Turn off all UI from being shown to the
// user during the operation. This includes error, confirmation,
// and progress dialogs.
//
Result := lFileOperation.SetOperationFlags(FOF_NO_UI);
if Succeeded(Result) then
begin
//
// Create an IShellItem from the supplied source path.
//
Result := SHCreateItemFromParsingName(aSrcItem,
nil,
IShellItem, psiFrom);
if Succeeded(Result) then
begin
if aDest <> '' then
begin
//
// Create an IShellItem from the supplied
// destination path.
//
Result := SHCreateItemFromParsingName(aDest,
nil,
IShellItem, psiTo);
end;
if Succeeded(Result) then
begin
//
// Add the operation
//
Result := lFileOperation.CopyItem(psiFrom, psiTo, aNewName, nil);
psiTo := nil;
end;
psiFrom := nil;
end;
if Succeeded(Result) then
begin
//
// Perform the operation to copy the file.
//
Result := lFileOperation.PerformOperations;
end;
end;
//
// Release the IFileOperation interface.
//
lFileOperation := nil;
end;
CoUninitialize;
end;
end;
免责声明: Windows Vista和更高版本提供IFileOperation.CopyItem。 因此,上面的示例仅适用于Delphi 2010(和2009?)。 由于我使用的是Delphi 7,因此无法编译此代码,因为我缺少单元ShlObj的最新版本。 幸运的是,使用来自Delphi的COM相当容易,因此转换示例并不重要。 我在CLSID上搜索了IFileOperation,所以我不知道它是否正确。
如果您真的希望它可以与Delphi 7一起使用,则必须具有IFileOperation的定义。 Jeroen提供的链接具有IShellItem的定义,但没有IFileOperation的定义。 如果您知道有人使用的是Delphi 2010版本,则可以要求他提供ShlObj.pas(但该文件已受版权保护,因此您必须自己翻译Shobjidl.h或等待其他人进行操作,您可以检查JEDI项目)。
当这一切看起来非常复杂时,请尝试Windows Api调用CopyFile
。
在我最初回答时,
T布鲁特 ·马丁斯·
斯图亚尼 (Bruno Martins
Stuani)在
TFileOperation CopyItem Delphi搜索上的第一个热门
文章是有关使用
IFileOperation及其
CopyItem方法的。
该帖子包括Delphi示例代码。
编辑 :从Delphi 2010开始,在ShlObj
单元中定义了IFileOperation
接口。
它取决于该单元中的许多其他内容,因此这里不是快速的“复制粘贴”(除了该单元已获得版权的事实)。
--jeroen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.