我要完成拷贝文件的功能,代码如下:(在USES 加入shellapi单元)
procedure TForm1.Button1Click(Sender: TObject);
var
   F:TShFileOpStruct;
begin
   F.Wnd:=Handle;
   F.wFunc:=FO-COPY;
   F.pForm:='c:\demo.txt';
   F.pTo:='d:\test2.txt';
   F.fFlags:=FOF-ALLOWUNDO OR FOF-RENAMEONCOLLISION;
   if ShFileOperation(F)<>0 then
      ShowMessage('文件拷贝失败!');
end;报错如下:
[Error] Unit1.pas(31): Undeclared identifier: 'FO'
[Error] Unit1.pas(32): Undeclared identifier: 'pForm'
[Error] Unit1.pas(32): Incompatible types
[Error] Unit1.pas(34): Undeclared identifier: 'FOF'
[Error] Unit1.pas(34): Undeclared identifier: 'RENAMEONCOLLISION'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
怎么回事呢?

解决方案 »

  1.   

    你将出借的动方用手工再输入一遍即可,FO-COPY改为FO_COPY
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
       F:TShFileOpStruct;
    begin
       F.Wnd:=Handle;
       F.wFunc:=FO_COPY;
       F.pFrom:='c:\demo.txt';
       F.pTo:='d:\test2.txt';
       F.fFlags:=FOF_ALLOWUNDO OR FOF_RENAMEONCOLLISION;
       if ShFileOperation(F)<>0 then
          ShowMessage('文件拷贝失败!');
    end;
      

  3.   

    用BOOL CopyFile(    LPCTSTR lpExistingFileName, // pointer to name of an existing file 
        LPCTSTR lpNewFileName, // pointer to filename to copy to 
        BOOL bFailIfExists  // flag for operation if file exists 
       );
      

  4.   

    我用了另外两种方式完成文件拷贝功能:
    方式1:
    Procedure TForm1.Button1Click(Sender: TObject);
    var
    FromF,ToF : file of byte;
    Buffer : array[0..4096] of char;
    NumRead : integer;
    FileLength : longint;
    begin
    Source:='c:\demo.txt';
    Destination:='d:\demo.txt'
    AssignFile(FromF,Source);
    reset(FromF);
    AssignFile(ToF,Destination);
    rewrite(ToF);
    FileLength:=FileSize(FromF);    //源文件的大小
    With Progressbar1 do
    begin
      
      Min := 0;
      Max := FileLength;
      while FileLength > 0 do
         begin
           BlockRead(FromF,Buffer[0],SizeOf(Buffer),NumRead);  
           FileLength := FileLength - NumRead;
           BlockWrite(ToF,Buffer[0],NumRead);  
           Position := Position + NumRead;  
         end;
        CloseFile(FromF); 
        CloseFile(ToF);
    end;
    end;
    该程序是成功的,现在我把源文件Source:='c:\demo.txt';换成网络某台机上的文件
    Source:='\\fornet-177\update\demo.txt';便报错如下
    Project Project1.exe raised exception class EInOutError with message
    'File access denied'.Process stopped.Use Step or Run to continue.
    怎么不能拷贝网络上的文件么?方式2:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      S,T: TFileStream; {文件流}
      SourceFileName,DestFileName:String;{源文件和目的文件名}
      IniFile:TIniFile;
      FileName:string;
    begin
          SourceFileName:='\\fornet-177\update\demo.txt';
          DestFileName:='d:\test.txt';
           S:=TFileStream.Create(SourceFileName, fmOpenRead );
         try
            T:= TFileStream.Create(DestFileName,fmOpenWrite or fmCreate );
            try
              T.CopyFrom(S,S.Size);
            finally
              T.Free;
            end;
         finally
         begin
            S.Free;
            showmessage('拷贝成功!');
         end;
         end;
    end;方式2可以拷贝本机或是网络上的文件,但是用方式2我想用进度条控件显示拷贝进度,该怎么做呢?