unit FmxUtils;<br><br>interface<br><br>uses SysUtils, Windows, Classes, Consts;<br><br>type<br> EInvalidDest = class(EStreamError);<br> 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> ShowCmd: Integer): THandle;<br><br>implementation<br><br>uses Forms, ShellAPI;<br><br>const<br> SInvalidDest = 'Destination %s does not exist';<br> SFCantMove = 'Cannot move file %s';<br><br>procedure CopyFile(const FileName, DestName: string);<br>var<br> CopyBuffer: Pointer; { buffer for copying }<br> BytesCopied: Longint;<br> Source, Dest: Integer; { handles }<br> Len: Integer;<br> Destination: TFileName; { holder for expanded destination name }<br>const<br> ChunkSize: Longint = 8192; { copy in 8K chunks }<br>begin<br> Destination := ExpandFileName(DestName); { expand the destination path }<br> if HasAttr(Destination, faDirectory) then { if destination is a directory... }<br> begin<br> Len := Length(Destination);<br> if Destination[Len] = '/' then<br> Destination := Destination + ExtractFileName(FileName) { ...clone file name }<br> else<br> Destination := Destination + '/' + ExtractFileName(FileName); { ...clone file name }<br> end;<br>GetMem(CopyBuffer, ChunkSize); { allocate the buffer }<br> try<br> Source := FileOpen(FileName, fmShareDenyWrite); { open source file }<br> if Source < 0 then raise EFOpenError.CreateFmt(SFOpenError, [FileName]);<br> try<br> Dest := FileCreate(Destination); { create output file; overwrite existing }<br> if Dest < 0 then raise EFCreateError.CreateFmt(SFCreateError, [Destination]);<br> try<br> repeat<br> BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); { read chunk }<br> if BytesCopied > 0 then { if we read anything... }<br> FileWrite(Dest, CopyBuffer^, BytesCopied); { ...write chunk }<br> until BytesCopied < ChunkSize; { until we run out of chunks }<br> finally<br> FileClose(Dest); { close the destination file }<br> end;<br> finally<br> FileClose(Source); { close the source file }<br> end;<br> finally<br> FreeMem(CopyBuffer, ChunkSize); { free the buffer }<br> end;<br>end;<br><br><br>{ MoveFile procedure }<br>{<br> Moves the file passed in FileName to the directory specified in DestDir.<br> Tries to just rename the file. If that fails, try to copy the file and<br> delete the original.<br><br> Raises an exception if the source file is read-only, and therefore cannot<br> be deleted/moved.<br>}<br><br>procedure MoveFile(const FileName, DestName: string);<br>var<br> Destination: string;<br>begin<br> Destination := ExpandFileName(DestName); { expand the destination path }<br> if not RenameFile(FileName, Destination) then { try just renaming }<br> begin<br> if HasAttr(FileName, faReadOnly) then { if it's read-only... }<br> raise EFCantMove.Create(Format(SFCantMove, [FileName])); { we wouldn't be able to delete it }<br> CopyFile(FileName, Destination); { copy it over to destination...}<br>// DeleteFile(FileName); { ...and delete the original }<br> end;<br>end;<br><br>{ GetFileSize function }<br>{<br> Returns the size of the named file without opening the file. If the file<br> doesn't exist, returns -1.<br>}<br><br>function GetFileSize(const FileName: string): LongInt;<br>var<br> SearchRec: TSearchRec;<br>begin<br> try<br> if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then<br> Result := SearchRec.Size<br> else Result := -1;<br> finally<br> SysUtils.FindClose(SearchRec);<br> end;<br>end;<br><br>function FileDateTime(const FileName: string): System.TDateTime;<br>begin<br> Result := FileDateToDateTime(FileAge(FileName));<br>end;<br><br>function HasAttr(const FileName: string; Attr: Word): Boolean;<br>var<br> FileAttr: Integer;<br>begin<br> FileAttr := FileGetAttr(FileName);<br> if FileAttr = -1 then FileAttr := 0;<br> Result := (FileAttr and Attr) = Attr;<br>end;<br><br>function ExecuteFile(const FileName, Params, DefaultDir: string;<br> ShowCmd: Integer): THandle;<br>var<br> zFileName, zParams, zDir: array[0..79] of Char;<br>begin<br> Result := ShellExecute(Application.MainForm.Handle, nil,<br> StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),<br> StrPCopy(zDir, DefaultDir),ShowCmd );<br>end;<br>end.<br>试试我给你的这个文件。