如何在程序中显示所选文件的大小?十万火急!!在线等!(20分)

  • 主题发起人 主题发起人 chiefwang
  • 开始时间 开始时间
C

chiefwang

Unregistered / Unconfirmed
GUEST, unregistred user!
如题!<br>不胜感激!
 
你是要显示出字节的大小吗?下面这个例子我改了一下,没有调试过,你参考一下吧<br>function TForm_MakeInstall.GetAllfile(source_path: string): Tstringlist;<br>var filelist: Tstringlist;<br>procedure FindAll(const Path: string);<br>var<br>&nbsp; sr: TSearchRec;<br>&nbsp; fr: Integer;<br>&nbsp; temp_str,strszie: string;<br>begin<br>&nbsp; fr := FindFirst(Path + '/*.*', faAnyFile, sr);<br>&nbsp; while fr = 0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if (sr.Attr = faDirectory) and (sr.Name &lt;&gt; '.') and (sr.Name &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp; FindAll(Path + '/' + sr.Name) //递归查找下一个目录<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; if (sr.Name &lt;&gt; '.') and (sr.Name &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; filelist.Append(copy(path, length(source_path) + 1, length(path) - length(source_path)) + '/' + sr.name);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage(copy(path,length(source_path)+1,length(path)-length(source_path)) + '/' + sr.name);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; temp_str := sr.name;<br>&nbsp; &nbsp; strszie:=Trim(IntToStr(sr.Size)); //这里就是文件大小了<br>FindNext(sr);<br>&nbsp; &nbsp; if temp_str = sr.Name then<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; end;<br>&nbsp; FindClose(sr);<br>end;<br><br>begin<br>&nbsp; filelist := Tstringlist.Create;<br>&nbsp; FindAll(source_path);<br>&nbsp; result := filelist;<br>end; &nbsp;<br>&nbsp;<br>
 
要这么麻烦啊!<br>我的程序已经有了,文件打开和文件选择功能了,只是我想当选择打开一个文件后能能够在label上显示大小。是不是有个API函数啊?
 
怎么没人接我帖啊?是不是太容易了!<br>大家帮帮忙啊!我是新手。<br>下面是我的构想,不过好象实现不了。<br>function GetFileSize(const fileName:string):integer;//获取文件大小<br>var f:Tfilestream;<br>begin<br>&nbsp; f:=tfilestream.Create(filename,fmopenread or fmsharedenynone);<br>&nbsp; result:=f.Size;<br>&nbsp; F.Free;<br>&nbsp; end;<br><br>procedure Form1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; if OpenDialog1.Execute<br>&nbsp; then<br>&nbsp; begin<br>&nbsp; &nbsp; FilePath:=OpenDialog1.FileName;<br>&nbsp; form.Label3.Caption:=OpenDialog1.FileName+#13#10'文件大小为:‘+??;//显示打开文件的路径和大小(??为不知该怎么加)<br>&nbsp; &nbsp; Button2.Enabled:=true;<br>&nbsp; &nbsp; Button3.Enabled:=true;<br>&nbsp; &nbsp; &nbsp;pagecontrol1.Show;<br>&nbsp; &nbsp; &nbsp;prbcut.Show;<br>&nbsp; &nbsp; &nbsp;memo1.Show;<br>&nbsp; &nbsp; &nbsp;panel1.Show;<br>&nbsp; end;<br>end;
 
form.Label3.Caption:=OpenDialog1.FileName+#13#10'文件大小为:‘+IntToStr(GetFileSize(Opendialog1.FileName));<br>得到的是字节数!
 
用流!!!!!!!!!!!!!!!!苯
 
楼上就说的不好<br>这是当时楼主的想法:<br>Delphi的例子:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp;f: file of Byte;<br>&nbsp; &nbsp;size: Longint;<br>&nbsp; &nbsp;S: string;<br>begin<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; begin<br>&nbsp; &nbsp; AssignFile(f, OpenDialog1.FileName);<br>&nbsp; &nbsp; Reset(f);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; size := FileSize(f);<br>&nbsp; &nbsp; &nbsp; S := 'File size in bytes: ' + IntToStr(size);<br>&nbsp; &nbsp; &nbsp; ShowMessage(s);<br>&nbsp; &nbsp; Finally<br>&nbsp; &nbsp; &nbsp; CloseFile(f);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
太好了,oldsheep35已经基本解决了我的问题!<br>可我还有个疑问想请教,用我那GetFilesize函数和oldsheep35的提示可以宣示大小,但是好象不是以K为单位的。怎么解决呢?比如:310K显示的是317496<br>谢谢啦!
 
由字节转换成K就是要除以1024<br>然后取近似值就可以了!<br>例如<br>317496 Div 1024=310.0546
 
多人接受答案了。
 
后退
顶部