怎样读取正在使用的文件大小 ( 积分: 100 )

  • 主题发起人 主题发起人 68686688
  • 开始时间 开始时间
6

68686688

Unregistered / Unconfirmed
GUEST, unregistred user!
请问怎样读取其他应用程序正在使用的文件的大小,先谢谢了。
 
请问怎样读取其他应用程序正在使用的文件的大小,先谢谢了。
 
转载:<br>具体到每一个文件的属性,你可以自己修改getfilelist函数,<br>function GetFileList(path:string;Attr:integer;FileList:TStringList):boolean;<br>var<br> &nbsp; SearchRec: TSearchRec;<br> &nbsp; i,total:integer;<br>begin<br> &nbsp; &nbsp; i:=FindFirst(path, attr, SearchRec);<br> &nbsp; &nbsp; if i &lt;&gt; 0 then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result := false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; while i = 0 do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//FileList.Add(SearchRec.Name);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;total:=SearchRec.size<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i:=FindNext(SearchRec);<br> &nbsp; &nbsp; end;<br> &nbsp; &nbsp; FindClose(SearchRec);<br> &nbsp; &nbsp; result := true;<br>end;<br><br>使用范例:<br>var<br> &nbsp; FileList:Tstringlist;<br>begin<br> &nbsp; filelist := Tstringlist.Create;<br> &nbsp; getfilelist(ExtractFilePath(Application.ExeName)+'*.exe',faAnyFile,filelist);//寻找程序所在目录下的所有*.dat文件<br>//do something with filelist<br> &nbsp; filelist.free;<br><br>end; <br><br><br>从TSearchRec结构里获得<br>TSearchRec的结构:<br>TSearchRec = record<br>Time: Integer;//Time contains the time stamp of the file.<br>Size: Integer;//Size contains the size of the file in bytes.<br>Attr: Integer;//Attr represents the file attributes of the file.<br>Name: TFileName;//Name contains the DOS filename and extension.<br>ExcludeAttr: Integer;<br>FindHandle: THandle;<br>FindData: TWin32FindData;//FindData contains additional information such as<br>//file creation time, last access time, long and short filenames.<br>end;
 
function GetFileSize(const FileName: string):Integer<br>var<br> &nbsp; SearchRec: TSearchRec;<br>begin<br> &nbsp; &nbsp; if FindFirst(FileName, faAnyFile, SearchRec) = 0 then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result := SearchRec.Size;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseFind(SearchRec);<br> &nbsp; &nbsp; end;<br>end;<br>这样就可以了。不过有些独占打开的文件可能返回的值是0
 
非常感谢两位。
 
function GetFileSize(const FileName: string):Integer<br>var<br> &nbsp; SearchRec: TSearchRec;<br>begin<br> &nbsp; &nbsp; Result := 0; //&lt;-这里加一句,否则如果FileName文件不存在可能返回不可预料的值作为结果:)一时倏忽,特补充上来。<br> &nbsp; &nbsp; if FindFirst(FileName, faAnyFile, SearchRec) = 0 then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result := SearchRec.Size;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CloseFind(SearchRec);<br> &nbsp; &nbsp; end;<br>end;
 
后退
顶部