用GetLogicalDriveStrings或FindFirstVoum两种方法获取主机驱动器列表

简介:

电子书截图:

代码A:

复制代码
 1 #include <windows.h>
 2 #include <stdlib.h>
 3 #include <stdio.h>
 4 
 5 #define BUFSIZE 1024
 6 
 7 BOOL GetDirverInfo(LPSTR szDrive);
 8 
 9 void main(void)
10 {
11     CHAR szLogicalDriveStrings[BUFSIZE];
12     PCHAR szDrive;
13 
14     ZeroMemory(szLogicalDriveStrings,BUFSIZE);
15 
16     GetLogicalDriveStrings(BUFSIZE - 1,szLogicalDriveStrings);
17     szDrive = (PCHAR)szLogicalDriveStrings;
18     do
19     {
20         if(!GetDirverInfo(szDrive))
21         {
22             printf("\nGet Volume Information Error: %d",GetLastError());
23         }
24         szDrive += (lstrlen(szDrive) + 1);
25     }
26     while(*szDrive != '\x00');
27 }
28 
29 BOOL GetDirverInfo(LPSTR szDrive)
30 {
31     UINT uDriveType;
32     DWORD dwVolumeSerialNumber;
33     DWORD dwMaximumComponentLength;
34     DWORD dwFileSystemFlags;
35     CHAR szFileSystemNameBuffer[BUFSIZE];
36     CHAR szDirveName[MAX_PATH];
37 
38     printf("\n%s\n",szDrive);
39     uDriveType = GetDriveType(szDrive);
40     switch(uDriveType)
41     {
42     case DRIVE_UNKNOWN:
43         printf("The drive type cannot be determined.");
44         break;
45     case DRIVE_NO_ROOT_DIR:
46         printf("The root path is invalid,for example,no volume mounted at the path.");
47         break;
48     case DRIVE_REMOVABLE:
49         printf("The drive is a type that has removable media,for example,a floppy drive or removable hard disk.");
50         break;
51     case DRIVE_FIXED:
52         printf("The drive is a type that cannot be removed,for example,a fixed hard drive.");
53         break;
54     case DRIVE_REMOTE:
55         printf("The drive is a remote (network) drive.");
56         break;
57     case DRIVE_CDROM:
58         printf("The drive is a CD-ROM drive.");
59         break;
60     case DRIVE_RAMDISK:
61         printf("The drive is a RAM disk.");
62         break;
63     default:
64         break;
65     }
66     if(!GetVolumeInformation(
67         szDrive,
68         szDirveName,
69         MAX_PATH,
70         &dwVolumeSerialNumber,
71         &dwMaximumComponentLength,
72         &dwFileSystemFlags,
73         szFileSystemNameBuffer,
74         BUFSIZE
75         ))
76     {
77         return FALSE;
78     }
79     if(0!=lstrlen(szDirveName))
80     {
81         printf ("\nDrive Name is %s\n",szDirveName);
82     }
83     printf("\nVolume Serial Number is %u",dwVolumeSerialNumber);
84     printf("\nMaximum component Length is %u",dwMaximumComponentLength);
85     printf("\nSystem Type is %s\n",szFileSystemNameBuffer);
86     if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
87     {
88         printf("The file system does not support volume mount points.\n");
89     }
90     if(dwFileSystemFlags & FILE_VOLUME_QUOTAS)
91     {
92         printf("The file system supports disk quotas.\n");
93     }
94     if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
95     {
96         printf("The file system supports case-sensitive file name.\n");
97     }
98     return TRUE;
99 }
复制代码

 

代码B:

复制代码
  1 #define _WIN32_WINNT 0x0501
  2 
  3 #include <Windows.h>
  4 #include <stdio.h>
  5 
  6 #define BUFSIZE  MAX_PATH
  7 BOOL GetDirverInfo(LPSTR szDrive);
  8 
  9 int main(void)
 10 {
 11     TCHAR buf[BUFSIZE];
 12     HANDLE hVol;
 13     BOOL bFlag;
 14 
 15     hVol = FindFirstVolume (buf, BUFSIZE);
 16 
 17     if (hVol == INVALID_HANDLE_VALUE)
 18     {
 19         printf("No volumes found!\n");
 20         return (-1);
 21     }
 22     GetDirverInfo(buf);
 23     while (FindNextVolume(
 24         hVol,
 25         buf,
 26         BUFSIZE
 27         ))
 28     {
 29         GetDirverInfo(buf);
 30     }
 31     bFlag = FindVolumeClose(hVol);
 32     return (bFlag);
 33 }
 34 
 35 BOOL GetDirverInfo(LPSTR szDrive)
 36 {
 37     UINT uDriveType;
 38     DWORD dwVolumeSerialNumber;
 39     DWORD dwMaximumComponentLength;
 40     DWORD dwFileSystemFlags;
 41     CHAR szFileSystemNameBuffer[BUFSIZE];
 42     CHAR szDirveName[MAX_PATH];
 43 
 44     printf("\n%s\n",szDrive);
 45     uDriveType = GetDriveType(szDrive);
 46     switch(uDriveType)
 47     {
 48     case DRIVE_UNKNOWN:
 49         printf("The drive type cannot be determined.");
 50         break;
 51     case DRIVE_NO_ROOT_DIR:
 52         printf("The root path is invalid,for example,no volume mounted at the path.");
 53         break;
 54     case DRIVE_REMOVABLE:
 55         printf("The drive is a type that has removable media,for example,a floppy drive or removable hard disk.");
 56         break;
 57     case DRIVE_FIXED:
 58         printf("The drive is a type that cannot be removed,for example,a fixed hard drive.");
 59         break;
 60     case DRIVE_REMOTE:
 61         printf("The drive is a remote (network) drive.");
 62         break;
 63     case DRIVE_CDROM:
 64         printf("The drive is a CD-ROM drive.");
 65         break;
 66     case DRIVE_RAMDISK:
 67         printf("The drive is a RAM disk.");
 68         break;
 69     default:
 70         break;
 71     }
 72     if(!GetVolumeInformation(
 73         szDrive,
 74         szDirveName,
 75         MAX_PATH,
 76         &dwVolumeSerialNumber,
 77         &dwMaximumComponentLength,
 78         &dwFileSystemFlags,
 79         szFileSystemNameBuffer,
 80         BUFSIZE
 81         ))
 82     {
 83         return FALSE;
 84     }
 85     if(0!=lstrlen(szDirveName))
 86     {
 87         printf ("\nDrive Name is %s\n",szDirveName);
 88     }
 89     printf("\nVolume Serial Number is %u",dwVolumeSerialNumber);
 90     printf("\nMaximum component Length is %u",dwMaximumComponentLength);
 91     printf("\nSystem Type is %s\n",szFileSystemNameBuffer);
 92     if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
 93     {
 94         printf("The file system does not support volume mount points.\n");
 95     }
 96     if(dwFileSystemFlags & FILE_VOLUME_QUOTAS)
 97     {
 98         printf("The file system supports disk quotas.\n");
 99     }
100     /*if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
101     {
102         printf("The file system supports case-sensitive file name.\n");
103     }*/
104     return TRUE;
105 }
复制代码

 

运行截图:

目录
相关文章
|
4天前
|
Windows
“快捷方式指向的驱动器或网络连接不可用” 解决方法
“快捷方式指向的驱动器或网络连接不可用” 解决方法
14 0
|
网络协议 安全
|
Windows 数据安全/隐私保护
|
安全 数据安全/隐私保护