如何获取电脑中所有的盘符?(100分)

  • 主题发起人 主题发起人 ww990
  • 开始时间 开始时间
W

ww990

Unregistered / Unconfirmed
GUEST, unregistred user!
比如我的电脑的分了n个分区,每个分区应该对应一个盘符(如C:/ d:/ E:/等),如何获取n的值,也就是检测我的电脑的共有多少盘。
 
遍历一下不就可以了
procedure TForm1.FormCreate(Sender: TObject);
var
buf:array [0..MAX_PATH-1] of char;
m_Result:Integer;
i:Integer;
str_temp:string;
begin
m_Result:=GetLogicalDriveStrings(MAX_PATH,buf);
for i:=0 to (m_Result div 4) do
begin
str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]);
ListBox1.Items.Add(str_temp);
end;
end;
 
用GetLogicalDrives最方便:)

Function GetDriveCount:Integer;
var
D1 : set of 0..25;
D2 : integer;
begin
Result:=0;
DWORD( D1 ) := Windows.GetLogicalDrives;
for D2 := 0 to 25 do
if (D2 in D1) then
Inc(Result);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var
buf:array [0..MAX_PATH-1] of char;
m_Result:Integer;
i:Integer;
str_temp:string;
begin
m_Result:=GetLogicalDriveStrings(MAX_PATH,buf);
for i:=0 to (m_Result div 4) -1 do
begin
str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]);
ListBox1.Items.Add(str_temp);
end;
showmessage(inttostr(ListBox1.Items.Count));//N的值
end;
 
如何直接获得盘符如C:/;D:/;E:/呢?
 
获取字符串形式的盘符可以用GetLogicalDriveStrings API:
The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.

DWORD GetLogicalDriveStrings(
DWORD nBufferLength, // size of buffer
LPTSTR lpBuffer // drive strings buffer
);
Parameters
nBufferLength
[in] Specifies the maximum size, in characters, of the buffer pointed to by lpBuffer. This size does not include the terminating null character. If this parameter is zero, lpBuffer is not used.
lpBuffer
[out] Pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, that end with a second null character. The following example shows the buffer contents with <null> representing the terminating null character.
c:/<null>d:/<null><null>
 

Similar threads

回复
0
查看
994
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
946
DelphiTeacher的专栏
D
后退
顶部