2010-07-14 154 views
0

我想用IFileOperation CopyItem复制文件从一个目录到另一个目录 是有一个简单的例子,在Delphi 7中使用IFileOperation?在Delphi 7

+1

你知道这是只打算从Vista和更高的工作吗?所以没有XP或Server 2003和更低? – 2010-07-14 11:57:07

+0

如果您有新问题,请不要编辑这个问题。你所做的改变没有任何意义,它使给定的答案无效。 – Will 2010-07-19 12:23:29

回答

3

在我原来的答复的时间,在 TFileOperation CopyItem Delphi search先打是 nice blog post由布鲁诺·马丁斯Stuani使用 IFileOperation及其 CopyItem方法。
该帖子包含Delphi示例代码。

编辑: 与从2010年的Delphi,所述IFileOperation接口在ShlObj单元定义。
这取决于在该单元不少其他东西,所以它不是一个快速的“复制粘贴”在这里(除了一个事实,即单位受版权保护)。

--jeroen

+0

博客文章是关于截取fileoperations,但没有解释如何使用IFileOperation.CopyItem? – 2010-07-14 12:35:49

+0

实际上,您的Google搜索中的第一次点击指向此页面,其中有跳转到另一个页面的第一个链接的指令,该页面指向跳转到ano第一个链接的页面。[STACKOVERFLOW] – 2010-07-14 12:36:51

+0

@Wouter:由于Delphi 7不包含IFileOperation的翻译,因此我将'use'理解为'具有IFileOperation接口的Delphi翻译。布鲁诺提供了这一点。 @The_Fox:在我写作的时候它是; StackOverflow有很棒的搜索引擎优化,所以通常它很快会在谷歌搜索结束。这就是为什么我发布了Bruno发布的链接。我很高兴你提供了一个更广泛的答案,并给你一个upvote。 - jeroen – 2010-07-14 12:46:35

7

我发现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

+0

这是一个很好的翻译,但你不需要删除接口。接口引用被重新计数,Delphi编译器将在函数结束时超出范围时自动释放它们。 – 2010-07-14 18:57:44

+0

@Mason Wheeler:我知道,但我只是翻译了MSDN的例子。因此,在MSDN示例称为发布的地方,我剔除了它们。只是为了展示你如何在Delphi中做到这一点,虽然它是多余的。 – 2010-07-15 06:41:13