开发者

using IFileOperation in delphi 7

I want to use IFileOperation CopyIt开发者_开发知识库em 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;

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.

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).

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


At the time of my original answer, the first hit on the TFileOperation CopyItem Delphi search is a nice blog post by Bruno Martins Stuani on using IFileOperation and its' CopyItem method.
The post includes Delphi sample code.

Edit: Starting with Delphi 2010, the IFileOperation interface is defined in the ShlObj unit.
It depends on quite a few other things in that unit, so it is not a quick "copy-paste" here (besides the fact that the unit is copyrighted).

--jeroen

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜