copyfile的用法?(100分)

  • 主题发起人 主题发起人 ABLE
  • 开始时间 开始时间
A

ABLE

Unregistered / Unconfirmed
GUEST, unregistred user!
我用copyfile拷贝文件,<br>bFlag:=CopyFile(PChar(sSource),Pchar(sFloppy),False);<br>if not bflag then<br>;<br>else<br>源文件肯定存在,目标盘也一切正常,<br>但奇怪的是有时执行返回true,有时执行返回false;<br>在单步跟踪时也有这种问题,这是为什么?谢谢
 
执行返回false原因是:<br>&nbsp; &nbsp; 和源文件同名的目标文件已经存在,并且目标文件是只读属性。
 
还有可能是目标文件已经存在,并且正在被操作系统使用中。
 
CopyFile 是独占模式打开文件。如果要copy的文件已经被其他程序打开,CopyFile就会失败。<br>
 
我是往软盘上拷贝一个Paradox文件,并没有被任何其他系统或程序占用
 
我是说源文件, 也要确保未被打开。
 
use shfileoperation
 
shfileoperation 还可以显示系统拷贝的画面,用它很好。
 
shfileoperation是什么?怎么使用?
 
function CopyFile(SourceName,TargetName:String):Boolean;<br>var<br>&nbsp; F:TShFileOpStruct;<br>begin<br>&nbsp; F.wnd:=InputForm.Handle;<br>&nbsp; F.wFunc:=FO_COPY; {操作方式}<br>&nbsp; F.pFrom:=PChar(SourceName+#0#0);<br>&nbsp; F.pTo:=PChar(TargetName+#0#0);<br>&nbsp; F.fFlags:=FOF_ALLOWUNDO OR FOF_RENAMEONCOLLISION;<br>&nbsp; result:= ShFileOperation(F)=0;<br>end;<br>
 
我适用过,但出现类似“系统文件复制有误”的对话框,请问如何解决??
 
shfileoperation属于哪个unit?<br>uses 哪个unit?
 
uses shellapi;
 
我也添加了 uses shellapi<br>&nbsp;但问题仍未解决
 
莫名其妙?!<br>ABLE:如果你还要继续讨论请定期提前你的帖子,如果不想继续讨论请结束帖子。
 
用shifileopertion()真的可以,我用过的,你好好读读帮助
 
unit FmxUtils;<br><br>interface<br><br>uses SysUtils, Windows, Classes, Consts;<br><br>type<br>&nbsp; EInvalidDest = class(EStreamError);<br>&nbsp; EFCantMove = class(EStreamError);<br><br>procedure CopyFile(const FileName, DestName: string);<br>procedure MoveFile(const FileName, DestName: string);<br>function GetFileSize(const FileName: string): LongInt;<br>function FileDateTime(const FileName: string): TDateTime;<br>function HasAttr(const FileName: string; Attr: Word): Boolean;<br>function ExecuteFile(const FileName, Params, DefaultDir: string;<br>&nbsp; ShowCmd: Integer): THandle;<br><br>implementation<br><br>uses Forms, ShellAPI;<br><br>const<br>&nbsp; SInvalidDest = 'Destination %s does not exist';<br>&nbsp; SFCantMove = 'Cannot move file %s';<br><br>procedure CopyFile(const FileName, DestName: string);<br>var<br>&nbsp; CopyBuffer: Pointer; { buffer for copying }<br>&nbsp; BytesCopied: Longint;<br>&nbsp; Source, Dest: Integer; { handles }<br>&nbsp; Len: Integer;<br>&nbsp; Destination: TFileName; { holder for expanded destination name }<br>const<br>&nbsp; ChunkSize: Longint = 8192; { copy in 8K chunks }<br>begin<br>&nbsp; Destination := ExpandFileName(DestName); { expand the destination path }<br>&nbsp; if HasAttr(Destination, faDirectory) then { if destination is a directory... }<br>&nbsp; begin<br>&nbsp; &nbsp; Len := &nbsp;Length(Destination);<br>&nbsp; &nbsp; if Destination[Len] = '/' then<br>&nbsp; &nbsp; &nbsp; Destination := Destination + ExtractFileName(FileName) { ...clone file name }<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Destination := Destination + '/' + ExtractFileName(FileName); { ...clone file name }<br>&nbsp; end;<br>GetMem(CopyBuffer, ChunkSize); { allocate the buffer }<br>&nbsp; try<br>&nbsp; &nbsp; Source := FileOpen(FileName, fmShareDenyWrite); { open source file }<br>&nbsp; &nbsp; if Source &lt; 0 then raise EFOpenError.CreateFmt(SFOpenError, [FileName]);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; Dest := FileCreate(Destination); { create output file; overwrite existing }<br>&nbsp; &nbsp; &nbsp; if Dest &lt; 0 then raise EFCreateError.CreateFmt(SFCreateError, [Destination]);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if BytesCopied &gt; 0 then { if we read anything... }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }<br>&nbsp; &nbsp; &nbsp; &nbsp; until BytesCopied &lt; ChunkSize; { until we run out of chunks }<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; FileClose(Dest); { close the destination file }<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; FileClose(Source); { close the source file }<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeMem(CopyBuffer, ChunkSize); { free the buffer }<br>&nbsp; end;<br>end;<br><br><br>{ MoveFile procedure }<br>{<br>&nbsp; Moves the file passed in FileName to the directory specified in DestDir.<br>&nbsp; Tries to just rename the file. &nbsp;If that fails, try to copy the file and<br>&nbsp; delete the original.<br><br>&nbsp; Raises an exception if the source file is read-only, and therefore cannot<br>&nbsp; be deleted/moved.<br>}<br><br>procedure MoveFile(const FileName, DestName: string);<br>var<br>&nbsp; Destination: string;<br>begin<br>&nbsp; Destination := ExpandFileName(DestName); { expand the destination path }<br>&nbsp; if not RenameFile(FileName, Destination) then { try just renaming }<br>&nbsp; begin<br>&nbsp; &nbsp; if HasAttr(FileName, faReadOnly) then &nbsp;{ if it's read-only... }<br>&nbsp; &nbsp; &nbsp; raise EFCantMove.Create(Format(SFCantMove, [FileName])); { we wouldn't be able to delete it }<br>&nbsp; &nbsp; &nbsp; CopyFile(FileName, Destination); { copy it over to destination...}<br>// &nbsp; &nbsp; &nbsp;DeleteFile(FileName); { ...and delete the original }<br>&nbsp; end;<br>end;<br><br>{ GetFileSize function }<br>{<br>&nbsp; Returns the size of the named file without opening the file. &nbsp;If the file<br>&nbsp; doesn't exist, returns -1.<br>}<br><br>function GetFileSize(const FileName: string): LongInt;<br>var<br>&nbsp; SearchRec: TSearchRec;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then<br>&nbsp; &nbsp; &nbsp; Result := SearchRec.Size<br>&nbsp; &nbsp; else Result := -1;<br>&nbsp; finally<br>&nbsp; &nbsp; SysUtils.FindClose(SearchRec);<br>&nbsp; end;<br>end;<br><br>function FileDateTime(const FileName: string): System.TDateTime;<br>begin<br>&nbsp; Result := FileDateToDateTime(FileAge(FileName));<br>end;<br><br>function HasAttr(const FileName: string; Attr: Word): Boolean;<br>var<br>&nbsp;FileAttr: Integer;<br>begin<br>&nbsp; FileAttr := FileGetAttr(FileName);<br>&nbsp; if FileAttr = -1 then FileAttr := 0;<br>&nbsp; Result := (FileAttr and Attr) = Attr;<br>end;<br><br>function ExecuteFile(const FileName, Params, DefaultDir: string;<br>&nbsp; ShowCmd: Integer): THandle;<br>var<br>&nbsp; zFileName, zParams, zDir: array[0..79] of Char;<br>begin<br>&nbsp; Result := ShellExecute(Application.MainForm.Handle, nil,<br>&nbsp; &nbsp; StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),<br>&nbsp; &nbsp; StrPCopy(zDir, DefaultDir),ShowCmd );<br>end;<br>end.<br>试试我给你的这个文件。
 
接受答案了.
 
后退
顶部