GetDiskFreeSpace为怎么只能读对C:/的空间其它的盘读的不对?(20分)

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

caoliu

Unregistered / Unconfirmed
GUEST, unregistred user!
一个Server端,一个Client端,之间的连结通讯已解决;<br>但是通过Client端读Server端host的磁盘空间,只能读对C:/的空间其它的盘读的不对?<br>把Client端和Server端放在一台host上结果一样;<br>我在Server端的.ServerSocket1ClientRead写的代码如下:<br>procedure TForm1.ServerSocket1ClientRead(Sender: TObject;<br>&nbsp; Socket: TCustomWinSocket);<br>&nbsp;var<br>&nbsp; &nbsp;Receive,send :String;<br>&nbsp; &nbsp;drivers :boolean;<br>&nbsp; &nbsp;sec, byte1, space1,space2:longword;<br>begin<br>&nbsp; &nbsp;Receive :=socket.ReceiveText;<br>&nbsp; &nbsp;drivers:= GetDiskFreeSpace(pchar(Receive), sec, byte1,space1,space2);<br>&nbsp; &nbsp;if drivers = true then<br>&nbsp; &nbsp; &nbsp;space2 :=space2 * sec * byte1<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;space2 := 0;<br>&nbsp; &nbsp;send := Formatfloat('###,##0',space2);<br>&nbsp; &nbsp;socket.SendText(send);<br>end;<br><br>end.<br>
 
GetDiskFreeSpace不支持2G以上的硬盤﹐你看其他盤符有沒有超過2G﹖
 
谁说的我的C:/就超过5G
 
试试我这个函数!<br><br>function GetClusterSize(RootDir: string): Integer;<br>var<br>&nbsp; &nbsp;SectorPerCluster, BytesPerSector,<br>&nbsp; &nbsp;FreeClusters, TotalClusterCount: Integer;<br>&nbsp; &nbsp;TempS: string;<br>begin<br>&nbsp; &nbsp;TempS := RootDir;<br>&nbsp; &nbsp;if Length(TempS) &gt; 3 then<br>&nbsp; &nbsp; &nbsp; Delete(TempS, 4, Length(TempS)-3);<br>&nbsp; &nbsp;GetDiskFreeSpace(PChar(TempS), SectorPerCluster,<br>&nbsp; &nbsp; &nbsp; BytesPerSector, FreeClusters, TotalClusterCount);<br>&nbsp; &nbsp;GetClusterSize := SectorPerCluster * BytesPerSector;<br>end;<br><br>
 
FUNCTION FreeSpace(Drive: BYTE): Integer;<br>&nbsp; VAR<br>&nbsp; &nbsp; BytesPerCluster &nbsp;: Integer;<br>&nbsp; &nbsp; RootPath &nbsp; &nbsp; &nbsp; &nbsp; : ARRAY[0..4] OF CHAR;<br>&nbsp; &nbsp; RootPtr &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: pChar;<br>&nbsp; &nbsp; SectorsPerCluster: &nbsp;DWORD ; &nbsp; &nbsp;// for both D3 and D4<br>&nbsp; &nbsp; BytesPerSector &nbsp; : &nbsp;DWORD;<br>&nbsp; &nbsp; FreeClusters &nbsp; &nbsp; : &nbsp;DWORD;<br>&nbsp; &nbsp; TotalClusters &nbsp; &nbsp;: &nbsp;DWORD;<br>BEGIN<br>&nbsp; RootPtr := NIL;<br>&nbsp; IF &nbsp; Drive &gt; 0<br>&nbsp; THEN BEGIN<br>&nbsp; &nbsp; StrCopy(RootPath, 'A:/');<br>&nbsp; &nbsp; RootPath[0] := CHR(Drive + ORD('A') - 1);<br>&nbsp; &nbsp; RootPtr := RootPath<br>&nbsp; END;<br>&nbsp; IF &nbsp; GetDiskFreeSpace(RootPtr, SectorsPerCluster,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BytesPerSector, FreeClusters, TotalClusters)<br>&nbsp; THEN BEGIN<br>&nbsp; &nbsp; BytesPerCluster := SectorsPerCluster * BytesPerSector;<br><br>&nbsp; &nbsp; RESULT := BytesPerCluster * FreeClusters<br>&nbsp; END<br>&nbsp; ELSE RESULT := -1<br>END;<br>
 
<br>&nbsp; &nbsp;Receive,send :String;<br>&nbsp; &nbsp;drivers :boolean;<br>&nbsp; &nbsp;sec, bytes1, space1,space2:longword;<br>&nbsp; &nbsp;byte1:int64;<br>begin<br>&nbsp; &nbsp;Receive :=socket.ReceiveText;<br>&nbsp; &nbsp;drivers:= GetDiskFreeSpace(pchar(Receive), sec, bytes1,space1,space2);<br>&nbsp; &nbsp;if drivers = true then<br>&nbsp; &nbsp; &nbsp;byte1 :=space2 * 4<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;space2 := 0;<br>&nbsp; &nbsp;send := Formatfloat('###,##0',byte1)+'kb';<br>&nbsp; &nbsp;socket.SendText(send);<br>end;<br><br>我找到了一个好方法,用KB 代替字节;我设一个变量:byte1:int64;<br>因为运行时sec=8,bytes1=512 这样byte1=space2*4(注意4*1024=8*512);<br>一切OK.
 
后退
顶部