SDK遍歷驅動器並獲取驅動屬性

提示這裏分成兩個文件,一個是GetDriveInfo.h,另一個是APIFinfFirstVolume.cpp

//APIFinfFirstVolume.cpp 代碼如下

/*
// 在這個程序裏面。顯示的驅動不是如C:\這樣的
// 而是直接顯示 物理驅動的唯一標示,
// 每個都是不一樣的請仔細看清楚了!
//  這些都是 保存驅動器名稱的內存緩存區
// \\?\Volume{56ffabb3-0ce3-11e1-a106-806d6172696f}\  C:\
// \\?\Volume{56ffabb4-0ce3-11e1-a106-806d6172696f}\  D:\
// \\?\Volume{56ffabb5-0ce3-11e1-a106-806d6172696f}\  E:\
// 不同之處在這裏,向上看
http://wenwen.soso.com/z/q254494002.htm?sp=1001
*/
/*****************************
// 遍歷驅動器並獲取驅動屬性
*****************************/
/*宏定義*/
#define _WIN32_WINNT 0x0501
//#define BUFSIZE MAX_PATH
/*頭文件*/
#include <windows.h>
#include <stdio.h>
#include "GetDriveInfo.h" //自己寫的頭文件


/*************************
//功能:應用程序主函數,遍歷驅動器並調用
// GetDriverInfo 獲取驅動器屬性
*************************/
int main(void)
{
 TCHAR buf[BUFSIZE];  //卷標信息  //
 HANDLE hVol;         //卷遍歷
 BOOL bFlag;

 //ZeroMemory(buf, MAX_PATH);
 hVol = FindFirstVolume(buf,BUFSIZE);//
 if(hVol == INVALID_HANDLE_VALUE)
 {
  printf(TEXT("NO Volume Found!\n"));
  return -1;
 }
 GetDriverInfo(buf);
 //printf("\n\n%s\n",szDriveName);
 while( FindNextVolume(
  hVol,
  buf,
  BUFSIZE))//
 {
  
  GetDriverInfo(buf);
 }
 bFlag = FindVolumeClose( hVol );
 return (bFlag);
}

//---------------------------------------------------------------------------------------------------------------

//---------------------------GetDriverInfo.h---------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------

//GetDriveInfo.h代碼如下


/*宏定義*/
#define BUFSIZE 1024
/*函數聲明*/
BOOL GetLogicDriverInfo(LPSTR szDrive);

/*************************
//BOOL GetDriverInfo(LPSTR szDrive)
//功能:
//參數:
//指明要獲取屬性的驅動器的根路徑,如C:\
//返回BOOL值:表示是否成功
*************************/

BOOL GetDriverInfo(LPSTR szDrive)
{
 UINT uDriveType;
 DWORD dwVolumeSerialNumber;
 DWORD dwMaximumComponentLenght;
 DWORD dwFileSystemFlags;
 CHAR szFileSystemNameBuffer[ BUFSIZE];
 CHAR szDriveName[MAX_PATH]; //驅動器名稱
 printf("\n\n%s\n",szDrive);
 uDriveType = GetDriveType(szDrive);  //獲取驅動類型
 switch(uDriveType)
 {
 case DRIVE_UNKNOWN:     //未知驅動器
  printf("The Driver type cannot be determined.");
  break;
 case DRIVE_NO_ROOT_DIR: //無根目錄的驅動器
  printf("The root path is invalid, no volume is mounted at the path.");
  break;
 case DRIVE_REMOVABLE:   //不可移除的驅動器
  printf("The drive is a type that removable media,for example,a floppy drive or removable hard disk.");
  break;
 case DRIVE_FIXED:       //固定驅動器
  printf("The drive is a type that cannot be removable,for example,a fixed hard drive.");
  break;
 case DRIVE_REMOTE:      //遠程驅動器
  printf("The drive is a remote (network) drive.");
  break;
 case DRIVE_CDROM:       //光驅
  printf("The drive is a CD-ROM drive.");
 case DRIVE_RAMDISK:     //隨機存儲驅動
  printf("The drive is a ram disk.");
  break;
 default:
   break;
 }

 //無法獲取驅動信息 返回BOOL值
 if( !GetVolumeInformation (
  szDrive,                    //輸入參數,指向所要獲取屬性的驅動器的根路徑字符串
  szDriveName,                //輸出參數,返回驅動器名稱
  MAX_PATH,                   //輸入參數,內存緩衝區大小
  &dwVolumeSerialNumber,      //輸出參數,存儲驅動器序列號
  &dwMaximumComponentLenght,  //輸出參數,返回文件系統所支持的文件組成部分的最大值
  &dwFileSystemFlags,         //輸出參數,判斷多種驅動的屬性值,用在 case 那裏,如FILE_VOLUME_QUOTAS
  szFileSystemNameBuffer,     //輸出參數,表示文件系統類型,如NTFS,FAT32,CDFS
  BUFSIZE                     //緩衝區大小
  ))
 {
  return FALSE;
 }

 if(0 != lstrlen(szDriveName))
 {
  printf("\nDrive Name is %s\n",szDriveName);
 }
 printf("\nVolume Serial Number is %u",dwVolumeSerialNumber); //空間大小
 printf("\nMaximum Componet Lenght is %u",dwMaximumComponentLenght); //最大組件長度 (命名長度)
 printf("\nSystem Type is %s\n",szFileSystemNameBuffer);

 if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
 {
  printf("The file system does not support volume mount points.\n");
 }

 else if(dwFileSystemFlags & FILE_VOLUME_QUOTAS)
 {
  printf("The file system supports disk quotas.\n");
 }

 else if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
 {
  printf("The file system supports case-senstive file names.\n");
 }
 //use this method get other infomation
 /*if(dwFileSystemFlags & )
 {
  printf("");
 }
 //
 printf("Other....\n");
 */
    return TRUE; 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章