读取全部的驱动器的信息

王朝厨房·作者佚名  2007-01-04
窄屏简体版  字體: |||超大  

作者:安静

VC6.0 W2K编译通过

这类小程序,能用的地方很多.所以就写一个完整的代码

给大家参考参考

#include "stdafx.h"

#include <iostream.h>

const DWORD MAXLEN = 100;

void ShowDriveInfo(LPTSTR drive)

{

//输出设备类型

UINT result;

result = GetDriveType(drive);

if (result == DRIVE_REMOVABLE)

cout << "可移动设备" ;

else if (result == DRIVE_FIXED)

cout << "硬盘";

else if (result == DRIVE_REMOTE)

cout << "网络驱动器" ;

else if (result == DRIVE_CDROM )

cout << " 光驱";

else if (result == DRIVE_RAMDISK)

cout << "Ram Disk";

else if (result == DRIVE_UNKNOWN)

cout << "未知的设备";

else

return;

cout << ’\t’;

//给出空间信息

unsigned __int64 i64FreeBytesToCaller;

unsigned __int64 i64TotalBytes;

unsigned __int64 i64FreeBytes;

DWORD dwSectPerClust;

DWORD dwBytesPerSect;

DWORD dwFreeClusters;

DWORD dwTotalClusters;

DWORD tempTotal;

DWORD tempFree;

BOOL fResult;

typedef DWORD (WINAPI * GETDISKFREESPACEEX)(LPCTSTR ,

PULARGE_INTEGER ,

PULARGE_INTEGER ,

PULARGE_INTEGER );

GETDISKFREESPACEEX pGetDiskFreeSpaceEx;

pGetDiskFreeSpaceEx = (GETDISKFREESPACEEX)GetProcAddress( GetModuleHandle("kernel32.dll"),

"GetDiskFreeSpaceExA");

if (pGetDiskFreeSpaceEx) //如果是Windows NT and Windows 2000使用 GetDiskFreeSpaceEx

{

fResult = pGetDiskFreeSpaceEx ( (LPCTSTR)drive,

(PULARGE_INTEGER)&i64FreeBytesToCaller,

(PULARGE_INTEGER)&i64TotalBytes,

(PULARGE_INTEGER)&i64FreeBytes);

tempTotal = i64TotalBytes/1024 ;

tempFree = i64FreeBytes/1024;

}

else //如果是Windows 95 OSR2 and Windows 98 使用 GetDiskFreeSpace

{

fResult = GetDiskFreeSpace (drive,

&dwSectPerClust,

&dwBytesPerSect,

&dwFreeClusters,

&dwTotalClusters);

tempTotal = dwTotalClusters*dwBytesPerSect*dwSectPerClust/1024;

tempFree = dwFreeClusters*dwSectPerClust*dwBytesPerSect/1024;

}

if(fResult)

{

cout<<"全部磁盘容量是 "<<((float)(tempTotal)/1024/1024)<<"GB\t";

cout<<"空余磁盘容量是 "<<((float)(tempFree)/1024/1024)<<"GB\t";

}

cout<<endl;

//像光驱,软盘,不放盘的时候,GetDiskFreeSpace(Ex)会出错.

}

void GetAllDrive()

{

int len = -1;

char drive[4] ="AAA" ;

LPTSTR lpDriveString = new char[MAXLEN];

DWORD dwBufferLen = MAXLEN ;

len = GetLogicalDriveStrings(dwBufferLen,lpDriveString); //取得全部的盘符

if (len < 0)

cout << "操作失败" <<endl;

if (len > MAXLEN)

cout << "Buffer不足" <<endl;

if (len > 0)

{

for (int i = 0;i < len;i = i+4) //得到的盘符有4个字符组成 A:\(null)

{

drive[0] = lpDriveString[i]; //A

drive[1] = lpDriveString[i+1];//:

drive[2] = lpDriveString[i+2];// 第三个\

//drive[3] = ’\0’; // 第四个为null字符

cout<<drive<<’\t’;

ShowDriveInfo(drive);

}

}

/////////////////////////////////////////////

//另外一种方法.

///////////////////////////////////////////////

/*

char D = ’A’;

char Drive[4]="X:\\";

for(int i = 0; i < 26;i++)

{

Drive[0] = (char)(D+i);

cout<<Drive<<’\t’;

ShowDriveInfo(Drive);

}

*/

}

void main()

{

GetAllDrive();

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航