原型:DWORD GetLogicalDrivers (void);
说明:函数返回值是一个long型,将其用二进制显示时,其中第0位表示A盘,第1位表示B盘,当某位为1时说明存在这个盘,即00000011表示有A盘和B盘。
看这个例子
#include <windows.h>
int main()
{
DWORD dw;
TCHAR TC1[50],TC2[50];
dw = GetLogicalDrives();
wsprintf(TC1,"%ld",dw);
MessageBox(NULL,TC1,"十进制",MB_OK);
_itoa(dw,TC2,2);
MessageBox(NULL,TC2,"十进制",MB_OK);
return 0;
}
#include <windows.h>
int main()
{
DWORD dw;
TCHAR TC[50];
int count = 0;
dw = GetLogicalDrives();
while(dw != 0)
{
if( (dw&1) != 0)
{
count++;
}
dw >>= 1;
}
wsprintf(TC,"驱动器个数:%d",count);
MessageBox(NULL,TC,"我插了U盘的",MB_OK);
return 0;
}
判断某个盘符是否存在:
#include <windows.h>
int main()
{
DWORD dw;
TCHAR TC[50];
dw = GetLogicalDrives();
char ch;
ch = 'Q';
int n = ch - 'A';
if( (dw != 0) && (dw >>= n) != 0 )
{
wsprintf(TC,"%c 盘 存 在",ch);
}
else
{
wsprintf(TC,"%c 盘 不 存 在",ch);
}
MessageBox(NULL,TC,"判断 该盘 是否存在",MB_OK);
return 0;
}
获得盘符 并且加到CComboBox列表中
CString s;
DWORD dwDrives = ::GetLogicalDrives();
for (int i = 0; dwDrives != 0; i++, dwDrives >>= 1)
{
if ((dwDrives & 0x01) == 0x01)
{
s.Format(_T("%c:"), 'A' + i);
m_wndDevices.AddString(s);
}
}
小的控制台程序
#include <stdio.h>
#include <windows.h>
int main()
{
char Root[] = "C:\\";
char Volumelabel[20];
DWORD SerialNumber;
DWORD MaxCLength;
DWORD FileSysFlag;
char FileSysName[10];
GetVolumeInformation( "C:\\",Volumelabel,255,&SerialNumber,&MaxCLength,&FileSysFlag,FileSysName,255);
printf("卷标名称(Volumelabel) = %s\n",Volumelabel);
printf("卷标序列号(SerialNumber) = 0x%x\n",*(&SerialNumber));
printf("系统允许最大文件名长度(MaxCLength) = 0x%x\n",*(&MaxCLength));
printf("文件系统标识(FileSysFlag) = 0x%x\n",*(&FileSysFlag));
printf("文件系统名称(FileSysName) = %s\n",FileSysName);
system("pause");
return 0;
}