我想用IFileOperation CopyItem
复制文件从一个目录到另一个目录 是有一个简单的例子,在Delphi 7中使用IFileOperation?在Delphi 7
回答
在我原来的答复的时间,在
TFileOperation CopyItem Delphi search先打是
nice blog post由布鲁诺·马丁斯Stuani使用
IFileOperation及其
CopyItem方法。
该帖子包含Delphi示例代码。
编辑: 与从2010年的Delphi,所述IFileOperation
接口在ShlObj
单元定义。
这取决于在该单元不少其他东西,所以它不是一个快速的“复制粘贴”在这里(除了一个事实,即单位受版权保护)。
--jeroen
博客文章是关于截取fileoperations,但没有解释如何使用IFileOperation.CopyItem? – 2010-07-14 12:35:49
实际上,您的Google搜索中的第一次点击指向此页面,其中有跳转到另一个页面的第一个链接的指令,该页面指向跳转到ano第一个链接的页面。[STACKOVERFLOW] – 2010-07-14 12:36:51
@Wouter:由于Delphi 7不包含IFileOperation的翻译,因此我将'use'理解为'具有IFileOperation接口的Delphi翻译。布鲁诺提供了这一点。 @The_Fox:在我写作的时候它是; StackOverflow有很棒的搜索引擎优化,所以通常它很快会在谷歌搜索结束。这就是为什么我发布了Bruno发布的链接。我很高兴你提供了一个更广泛的答案,并给你一个upvote。 - jeroen – 2010-07-14 12:46:35
我发现MSDN documentation和它包括一个例子。下面是翻译成德尔福的例子:
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;
免责声明: IFileOperation.CopyItem可从Windows Vista和更高。所以上面的例子只适用于Delphi 2010(和2009?)。由于我使用的是Delphi 7,因此我无法编译它,因为我缺少ShlObj单元的最新版本。从Delphi使用COM很容易,因此转换示例并不是什么大问题。我搜索了IFileOperation的CLSID,所以我不知道它是否是正确的。
如果你真的希望这用Delphi 7工作,你必须有IFileOperation的定义。 Jeroen提供的链接具有IShellItem的定义,但不适用于IFileOperation。如果你知道某人有一个Delphi 2010的版本,你可以问他ShlObj.pas(但它是有版权的,所以你必须Shobjidl.h自己翻译或等待别人去做,你可以检查的项目JEDI)。
当这一切似乎很复杂,尝试Windows API调用,CopyFile
。
这是一个很好的翻译,但你不需要删除接口。接口引用被重新计数,Delphi编译器将在函数结束时超出范围时自动释放它们。 – 2010-07-14 18:57:44
@Mason Wheeler:我知道,但我只是翻译了MSDN的例子。因此,在MSDN示例称为发布的地方,我剔除了它们。只是为了展示你如何在Delphi中做到这一点,虽然它是多余的。 – 2010-07-15 06:41:13
- 1. 不能在Delphi 7
- 2. 用Delphi 7
- 3. utf8decode in delphi 7
- 4. Delphi 7 ODBC MySQL Win 7
- 5. Delphi 7上的Quicktime 7
- 6. 在delphi 7中清除timage.canvas
- 7. 在Delphi 7中绕过OutputDebugString?
- 8. DateTimePicker在Delphi 7中对齐
- 9. TIdTCPServer多播(Indy 9 + Delphi 7)
- 10. Delphi 7个中TGUID比较
- 11. Delphi 7 IDE的写脚本
- 12. Windows 7与Delphi的集成
- 13. Delphi 7中的病毒
- 14. Delphi 7 MySQL5.7 mysql.pas libmysql.dll UTF8
- 15. 如何在Delphi 7中使用delphi制作com界面
- 16. 更新Delphi 7应用程序在Delphi 2010上运行
- 17. 如何在Delphi 2010中查找IdMappedPortTCP Delphi 7
- 18. “特定于平台”在Delphi 7上的Delphi 6警告
- 19. 尝试使用TZDB ...如何在delphi 7
- 20. Delphi:在Windows 7中禁用TaskManager
- 21. Delphi 7在IIS外的ADO连接池
- 22. 在Delphi 7中的FormShow问题
- 23. 如何在Delphi 7中使用chm?
- 24. 在Delphi 7上监听串行端口
- 25. 如何在Delphi 7中更新WSDL_Imp.exe
- 26. 如何在Vista上安装Delphi 7
- 27. 在SQL中更新图像(Delphi 7)
- 28. CHR相当于对Unicode在Delphi 7
- 29. 在Delphi 7上离开ComboBox的问题
- 30. 将Delphi XE5工作区更改为Delphi 7
你知道这是只打算从Vista和更高的工作吗?所以没有XP或Server 2003和更低? – 2010-07-14 11:57:07
如果您有新问题,请不要编辑这个问题。你所做的改变没有任何意义,它使给定的答案无效。 – Will 2010-07-19 12:23:29