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

查看:133
本文介绍了在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?

推荐答案

我找到了

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非常容易,因此转换示例并不重要.我在CLSID上搜索了IFileOperation,所以我不知道它是否正确.

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天全站免登陆