用CopyFile拷贝文件出现怪事,现问题以有眉目!请高人破解指点!!! (93分)

  • 主题发起人 主题发起人 dok001
  • 开始时间 开始时间
D

dok001

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;用API函数CopyFile拷贝文件出现怪事:<br>1 程序基本没变,只是动了后缀,但以前拷贝图片文件很正常,现在拷贝多媒体文件(mid和mp3)能通过编译,但不响应.<br>2 还发现API函数CopyFile后面分号';'要不要都能通过编译.<br>百思不得其解,请高人指点<br>procedure Form1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; Label1.Caption:=edit10.Text;//用来观察源文件是否正确,实际显示是正确的<br>&nbsp; wjm:='C:/Program Files/slbh/'+SpinEdit3.Text+'.mid';<br>&nbsp; Label2.Caption:=wjm;;//用来观察目标文件是否正确,实际显示是正确的<br>&nbsp; CopyFile(pchar(edit10.Text),pchar(wjm),False);//这里分号';'要不要都能通过编译<br>&nbsp; end;
 
第一个问题可能是文件名错误<br>第二个问题,对于最后一行代码';'可以不写
 
用API:<br>function CopyFiles(const Source,Dest: string): boolean;<br>var<br>&nbsp; fo: TSHFILEOPSTRUCT;<br>begin<br>&nbsp; FillChar(fo, SizeOf(fo), 0);<br>&nbsp; with fo do<br>&nbsp; begin<br>&nbsp; &nbsp; Wnd := 0;<br>&nbsp; &nbsp; wFunc := FO_COPY;<br>&nbsp; &nbsp; pFrom := @source[1];<br>&nbsp; &nbsp; pTo :=pchar(dest);<br>&nbsp; &nbsp; fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR &nbsp; &nbsp;;<br>&nbsp; end;<br>&nbsp; Result := (SHFileOperation(fo) = 0);<br>end;
 
源文件和目标文件在哪?是把这段拷到程序的哪一个地方,请讲细些!
 
你那个文件名中含有text内容,是不是有可能你那个text中有空格,最好还是用<br>trim(wjm)干一下,要不就是文件根本无法找到。
 
用trim(wjm)试了,还不行
 
原文件有吗?
 
还有更怪的事,<br>为了看个究竟,我又按了三个Button观察,又出现一会可替换,有的文件不可替换(我发复试了几十次,各种组合都试),真出了怪了!<br><br>procedure Tpropotivce.Button9Click(Sender: TObject);<br>begin<br>&nbsp; wjm:='C:/Program Files/slbh/'+SpinEdit3.Text+'.mid';<br>&nbsp; MediaPlayer1.FileName:=wjm;<br>&nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; MediaPlayer1.open;<br>&nbsp; MediaPlayer1.Notify:=True ;<br>end;<br><br>procedure Tpropotivce.Button10Click(Sender: TObject);<br>begin<br>wjm:='C:/Program Files/slbh/'+SpinEdit3.Text+'.mid';<br>&nbsp; MediaPlayer1.FileName:=edit10.Text;<br>&nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; MediaPlayer1.open;<br>&nbsp; MediaPlayer1.Notify:=True ;<br>end;<br><br>procedure Tpropotivce.Button11Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp;CopyFile(pchar(edit10.Text),pchar(wjm),false);<br>end;
 
将原码贴上,请高人指点:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, Spin, MPlayer;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; MediaPlayer1: TMediaPlayer;<br>&nbsp; &nbsp; OpenDialog1: TOpenDialog;<br>&nbsp; &nbsp; SpinEdit1: TSpinEdit;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; Button5: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Edit1Change(Sender: TObject);<br>&nbsp; &nbsp; procedure SpinEdit1Change(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button5Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; wjm:string;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>opendialog1.InitialDir:='C:/';<br>&nbsp; OpenDialog1.Filter := '声音文件Midi files(*.mid)|*.MID';<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; Edit1.Text:=OpenDialog1.FileName;<br>&nbsp; &nbsp; MediaPlayer1.FileName:=OpenDialog1.FileName ;<br>&nbsp; &nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; &nbsp; MediaPlayer1.open;<br>&nbsp; &nbsp; MediaPlayer1.Notify:=True ;<br>&nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; CopyFile(pchar(edit1.Text),pchar(wjm),False);<br>end;<br><br>procedure TForm1.Edit1Change(Sender: TObject);<br>begin<br>&nbsp; &nbsp;Label2.Caption:=SpinEdit1.Text;<br>&nbsp; wjm:='C:/Program Files/slbh/'+SpinEdit1.Text+'.mid';<br>&nbsp; trim(wjm);<br>&nbsp; MediaPlayer1.FileName:=wjm;<br>&nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; MediaPlayer1.open;<br>&nbsp; MediaPlayer1.Notify:=True ;<br>end;<br><br>procedure TForm1.SpinEdit1Change(Sender: TObject);<br>begin<br>&nbsp; Label2.Caption:=SpinEdit1.Text;<br>&nbsp; wjm:='C:/Program Files/slbh/'+SpinEdit1.Text+'.mid';<br>&nbsp; trim(wjm);<br>&nbsp; MediaPlayer1.FileName:=wjm;<br>&nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; MediaPlayer1.open;<br>&nbsp; MediaPlayer1.Notify:=True ;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>MediaPlayer1.FileName:=edit1.Text;<br>&nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; MediaPlayer1.open;<br>&nbsp; MediaPlayer1.Notify:=True ;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>MediaPlayer1.FileName:=wjm;<br>&nbsp; MediaPlayer1.AutoOpen:=True ;<br>&nbsp; MediaPlayer1.open;<br>&nbsp; MediaPlayer1.Notify:=True ;<br><br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br>CopyFile(pchar(edit1.Text),pchar(wjm),false);<br>end;<br><br>end.<br>
 
都被人回答了
 
终于试出来了,就是有的可准确替换,有的不能用这种方法替换,哪位高人能解释一下,并请示范其它替换办法!!!
 
用 if fileexist(edit1.text) then 判斷文件是否存在<br>用 if copyfile(..) then 判斷是否成功<br>如果不成功,可能是<br>1.目的文件檔名錯誤,例如不存在的資料夾,或檔案有*或?字元<br>2.無法寫入(網路權限,空間不足)<br>3.目的文件已被其他程式開啟,未關檔<br>請用getlasterror及formatmessage查看錯誤訊息(WinApi)
 
{This way uses a File stream.}<br>Procedure FileCopy( Const sourcefilename, targetfilename: String );<br>Var<br>&nbsp; S, T: TFileStream;<br>Begin<br>&nbsp; S := TFileStream.Create( sourcefilename, fmOpenRead );<br><br>&nbsp; try<br>&nbsp; &nbsp; T := TFileStream.Create( targetfilename,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmOpenWrite or fmCreate );<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; T.CopyFrom(S, S.Size ) ;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; T.Free;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; S.Free;<br>&nbsp; end;<br>End;<br><br>{This way uses memory blocks for read/write.}<br>procedure FileCopy(const FromFile, ToFile: string);<br>&nbsp;var<br>&nbsp; FromF, ToF: file;<br>&nbsp; NumRead, NumWritten: Word;<br>&nbsp; Buf: array[1..2048] of Char;<br>begin<br>&nbsp; AssignFile(FromF, FromFile);<br>&nbsp; Reset(FromF, 1); &nbsp;{ Record size = 1 }<br><br>&nbsp; AssignFile(ToF, ToFile); { Open output file }<br>&nbsp; Rewrite(ToF, 1); &nbsp;{ Record size = 1 }<br>&nbsp; repeat<br>&nbsp; &nbsp; BlockRead(FromF, Buf, SizeOf(Buf), NumRead);<br>&nbsp; &nbsp; BlockWrite(ToF, Buf, NumRead, NumWritten);<br>&nbsp; until (NumRead = 0) or (NumWritten &lt;&gt; NumRead);<br>&nbsp; CloseFile(FromF);<br>&nbsp; CloseFile(ToF);<br>end;<br><br>{This one uses LZCopy, which USES LZExpand.}<br>procedure CopyFile(FromFileName, ToFileName: string);<br>var<br>&nbsp; FromFile, ToFile: File;<br>begin<br>&nbsp; AssignFile(FromFile, FromFileName); { Assign FromFile to FromFileName }<br><br>&nbsp; AssignFile(ToFile, ToFileName); &nbsp; &nbsp; { Assign ToFile to ToFileName }<br>&nbsp; Reset(FromFile); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Open file for input }<br>&nbsp; try<br>&nbsp; &nbsp; Rewrite(ToFile); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Create file for output }<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; { copy the file an if a negative value is returned }<br>&nbsp; &nbsp; &nbsp; { raise an exception }<br>&nbsp; &nbsp; &nbsp; if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) &lt; 0<br>&nbsp; &nbsp; &nbsp; &nbsp; then<br>&nbsp; &nbsp; &nbsp; &nbsp; raise EInOutError.Create('Error using LZCopy')<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; CloseFile(ToFile); &nbsp;{ Close ToFile }<br><br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; CloseFile(FromFile); &nbsp;{ Close FromFile }<br>&nbsp; end;<br>end;<br>
 
接受答案了.
 
后退
顶部