现在有一个dpr编辑的无form程序,目前想实现copy功能,描述如下:    运行时将指定路径下的文件copy到本exe所在目录下,我试了好多次,都没有成功,查看help似乎copyfrom可行,但是我还是没办法弄出来。各位谁有类似的例子代码,能让我看看吗?或者适当提点一下也行,谢谢啦!

解决方案 »

  1.   

    CopyFile(PChar('C:\autoexec.bat'),  
                 PChar(ExtractFilepath(Application.ExeName)+'autoexec.bat') , 
                 True);
      

  2.   

    可以用SHFileOperation,这是一个ShellAPI,Delphi中没有提供直接文件拷贝的函数。
    要自己写Copy的函数,
    程序举例:可能不是很正确,提供思路。
    var
      F1,F2:TFile;
      Buf:array[0..1023] of Byte;
      Size:Integer;
    begin
      AssignFile(F1,'源文件名称');
      AssignFile(F2,'目标文件名称');
      ResetFile(F1);
      Rewrite(F2);
      Size:=1024;
      while not Eof(F1) do
      begin
        BlockRead(F1,Buf,Size);
        BlockWrite(F2,Buf,Size);
      end;
      CloseFile(F1);
      CloseFile(F1);
    end;
      

  3.   

    var  FromF, ToF: file;
      NumRead, NumWritten: Integer;
      Buf: array[1..2048] of Char;
    begin
      if OpenDialog1.Execute then                               { Display Open dialog box }
      begin
        AssignFile(FromF, OpenDialog1.FileName);
        Reset(FromF, 1); { Record size = 1 }
        if SaveDialog1.Execute then                              { Display Save dialog box}
        begin
          AssignFile(ToF, SaveDialog1.FileName); { Open output file }      Rewrite(ToF, 1); { Record size = 1 }
          Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF))
            + ' bytes...');
          repeat
            BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
            BlockWrite(ToF, Buf, NumRead, NumWritten);
          until (NumRead = 0) or (NumWritten <> NumRead);
            CloseFile(FromF);
            CloseFile(ToF);
        end;
      end;
    end;
      

  4.   

    谢谢各位啦,现在在教研室无法运行delphi调试,等会儿回去调试一下,如果没有什么问题就马上结帖!tommy_linux(津工之鸟)
    BlueTrees(蜗牛) 
    ahuige(灰不遛秋) 非常感谢!