在 delphi 7 中使用 IFileOperation [英] using IFileOperation in delphi 7

查看:30
本文介绍了在 delphi 7 中使用 IFileOperation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 IFileOperation CopyItem 将文件从一个目录复制到另一个目录delphi 7 有没有简单的例子?

I want to use IFileOperation CopyItem to copy a file from a directory to another directory is there a simple example in delphi 7?

推荐答案

我找到了 MSDN 文档,其中包含一个示例.以下是转换为 Delphi 的示例:

I found the MSDN documentation and it included an example. Here is the example translated to 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;

免责声明:IFileOperation.CopyItem 在 Windows Vista 及更高版本中可用.所以上面的例子只适用于 Delphi 2010(和 2009?).由于我在 Delphi 7 上,我无法编译它,因为我缺少单元 ShlObj 的最新版本.幸运的是,使用 Delphi 中的 COM 非常容易,因此转换示例并不是什么大问题.我在谷歌上搜索了 IFileOperation 的 CLSID,所以我不知道它是否正确.

Disclaimer: IFileOperation.CopyItem is available from Windows Vista and higher. So the above example will only work with Delphi 2010 (and 2009?). Since I am on Delphi 7 I cannot compile this because I am missing the latest version of unit ShlObj. Fortunatly using COM from Delphi is pretty easy, so converting the example wasn't a big deal. I googled the CLSID for IFileOperation, so I don't know if it is the right one.

如果你真的想让它与 Delphi 7 一起工作,你必须有一个 IFileOperation 的定义.Jeroen 提供的链接有 IShellItem 的定义,但没有 IFileOperation 的定义.如果你认识有Delphi 2010版本的人,你可以向他索要ShlObj.pas(但它是有版权的,所以你必须自己翻译Shobjidl.h或等别人来做,你可以查看JEDI项目).

If you really want this to work with Delphi 7, you must have a definition of IFileOperation. The link provided by Jeroen has a definition of IShellItem but not for IFileOperation. If you know someone with a Delphi 2010 version, you could ask him for ShlObj.pas (but it is copyrighted, so you have to translate Shobjidl.h yourself or wait for someone else to do it, you could check the JEDI project).

当这一切看起来很复杂时,请尝试调用 Windows Api,CopyFile.

When all this seems very complicated, try the Windows Api call, CopyFile.

这篇关于在 delphi 7 中使用 IFileOperation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆