Windows API 逐个逐个学MessageBox(5) 遍历驱动器并获取驱动器属性 GetLogicalDriveStrings、GetDriveTypeA...
生活随笔
收集整理的這篇文章主要介紹了
Windows API 逐个逐个学MessageBox(5) 遍历驱动器并获取驱动器属性 GetLogicalDriveStrings、GetDriveTypeA...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????? 首先還是貼代碼
????? VC++ Code:
/* *************************************《精通Windows API》 * 示例代碼* GetVolumeInfo.c* 4.2.1 遍歷驅動器并獲取驅動器屬性**************************************//* 頭文件 */ #include <windows.h> #include <stdlib.h> #include <stdio.h> #include<string.h> /* 預定義 */ #define BUFSIZE 1024 /* 函數申明 */ BOOL GetDirverInfo(LPSTR szDrive);/* ************************************* 功能 應用程序主函數,遍歷驅動器并調用* GetDirverInfo 獲取驅動器屬性**************************************/ void main(void) {CHAR szLogicalDriveStrings[BUFSIZE];PCHAR szDrive;CHAR lStr;int i ;ZeroMemory(szLogicalDriveStrings,BUFSIZE);// 獲取邏輯驅動器卷標名GetLogicalDriveStrings(BUFSIZE - 1,szLogicalDriveStrings); //strcpy(lStr,szLogicalDriveStrings,sizeof(szLogicalDriveStrings)/sizeof(CHAR));szDrive = (PCHAR)szLogicalDriveStrings;// 循環處理每個卷do{if(!GetDirverInfo(szDrive)){printf("\nGet Volume Information Error: %d", GetLastError());}szDrive += (lstrlen(szDrive)+1);//szDrive = szDrive +lstrlen(szDrive); }while(*szDrive!='\x00');scanf("%d",&i); }/* ************************************* BOOL GetDirverInfo(LPSTR szDrive)* 功能 獲取驅動器的屬性* 參數 LPSTR szDrive* 指明要獲取屬性的驅動器的根路徑 如 C:\* 返回值 BOOL 是否成功**************************************/ BOOL GetDirverInfo(LPSTR szDrive) {UINT uDriveType;DWORD dwVolumeSerialNumber;DWORD dwMaximumComponentLength;DWORD dwFileSystemFlags;TCHAR szFileSystemNameBuffer[BUFSIZE];printf("\n%s\n",szDrive);uDriveType = GetDriveType(szDrive);// 判斷類型switch(uDriveType){case DRIVE_UNKNOWN:printf("The drive type cannot be determined. ");break;case DRIVE_NO_ROOT_DIR:printf("The root path is invalid, for example, no volume is mounted at the path. ");break;case DRIVE_REMOVABLE:printf("The drive is a type that has removable media, for example, a floppy drive or removable hard disk. ");break;case DRIVE_FIXED:printf("The drive is a type that cannot be removed, 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. ");break;case DRIVE_RAMDISK:printf("The drive is a RAM disk. ");break;default:break;}if (!GetVolumeInformation(szDrive, NULL, 0,&dwVolumeSerialNumber,&dwMaximumComponentLength,&dwFileSystemFlags,szFileSystemNameBuffer,BUFSIZE)){return FALSE;}printf ("\nVolume Serial Number is %u",dwVolumeSerialNumber);printf ("\nMaximum Component Length is %u",dwMaximumComponentLength);printf ("\nSystem Type is %s\n",szFileSystemNameBuffer);if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS){printf ("The file system does not support volume mount points.\n");}if(dwFileSystemFlags & FILE_VOLUME_QUOTAS){printf ("The file system supports disk quotas.\n");}if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH){printf ("The file system supports case-sensitive file names.\n");}//you can use these value to get more informaion////FILE_CASE_PRESERVED_NAMES//FILE_CASE_SENSITIVE_SEARCH//FILE_FILE_COMPRESSION//FILE_NAMED_STREAMS//FILE_PERSISTENT_ACLS//FILE_READ_ONLY_VOLUME//FILE_SUPPORTS_ENCRYPTION//FILE_SUPPORTS_OBJECT_IDS//FILE_SUPPORTS_REPARSE_POINTS//FILE_SUPPORTS_SPARSE_FILES//FILE_UNICODE_ON_DISK//FILE_VOLUME_IS_COMPRESSED//FILE_VOLUME_QUOTASprintf("...\n");return TRUE; }?
?
???? 編譯后運行:
vb6 Code(轉自網上):
??????? 1).類模塊中:
Option Explicit'============= 類模塊里,名稱:GetDiskDrive =========== 'local variable(s) to hold property value(s) Private mvarDriveCount As Integer 'local copy'Value Name Meaning '------------------------------------------------------------ '0 DRIVE_UNKNOWN The drive type cannot be determined. '1 DRIVE_NO_ROOT_DIR The root directory does not exist. '2 DRIVE_REMOVABLE The disk can be removed from the drive. '3 DRIVE_FIXED The disk cannot be removed from the drive. '4 DRIVE_REMOTE The drive is a remote (network) drive. '5 DRIVE_CDROM The drive is a CD-ROM drive. '6 DRIVE_RAMDISK The drive is a RAM disk. '------------------------------------------------------------- Public Enum DriveTypeDRIVE_UNKNOWN = 0DRIVE_NO_ROOT_DIR = 1DRIVE_REMOVABLE = 2DRIVE_FIXED = 3DRIVE_REMOTE = 4DRIVE_CDROM = 5DRIVE_RAMDISK = 6 End EnumPrivate drvType(26) As Integer Private drvName(26) As String 'Attribute drvName.VB_VarDescription = "DriveName"Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Declare Function GetDriveTypeA Lib "kernel32" (ByVal nDrive As String) As LongPublic Function GetDriveType(ByVal n As Integer) As Integer 'Attribute GetDriveType.VB_Description = "獲得驅動器類型"GetDriveType = drvType(n) End FunctionPublic Function GetDriveName(ByVal n As Integer) As String 'Attribute GetDriveName.VB_Description = "獲得驅動器名稱"GetDriveName = drvName(n) End FunctionPublic Function GetDriveTypeName(ByVal n As Integer) As StringDim sTypeName As StringSelect Case nCase DRIVE_NO_ROOT_DIRsTypeName = "根目錄不存在"Case DRIVE_REMOVABLEsTypeName = "可移動磁盤,如軟盤"Case DRIVE_FIXEDsTypeName = "磁盤"Case DRIVE_REMOTEsTypeName = "磁盤映射"Case DRIVE_CDROMsTypeName = "CD-ROM"Case DRIVE_RAMDISKsTypeName = "U盤"Case ElsesTypeName = "未知類型"End SelectGetDriveTypeName = sTypeName End FunctionPublic Property Let DriveCount(ByVal vData As Integer) 'Attribute DriveCount.VB_Description = "驅動器個數" 'used when assigning a value to the property, on the left side of an assignment. 'Syntax: X.DriveCount = 5mvarDriveCount = vData End PropertyPublic Property Get DriveCount() As Integer 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.DriveCountDriveCount = mvarDriveCount End Property'With this class you can get the systme disk Drive Name and its type Private Sub Class_Initialize()Dim PathStr As String * 200Dim DriveStr As StringDim L, i As IntegermvarDriveCount = 0If GetLogicalDriveStrings(200, PathStr) <> 0 ThenDriveStr = Mid(PathStr, 1, InStr(1, PathStr, Chr$(0) & Chr$(0)))L = Len(DriveStr)For i = 1 To L Step 4mvarDriveCount = mvarDriveCount + 1drvName(mvarDriveCount) = Mid(DriveStr, i, 3)drvType(mvarDriveCount) = GetDriveTypeA(drvName(mvarDriveCount))Next iEnd If End Sub?
?????? 2):窗體中:
Private Sub Command4_Click()Dim i As IntegerDim drv As clsFileSystemAPIDim sResult As StringSet drv = New clsFileSystemAPIsResult = "驅動器總數為:" + CStr(drv.DriveCount) + vbCrLfFor i = 1 To drv.DriveCountsResult = sResult + drv.GetDriveName(i) + vbTab + drv.GetDriveTypeName(drv.GetDriveType(i)) + vbCrLfNext iLabel1.Caption = sResult End Sub?
??????? 運行:
?
轉載于:https://www.cnblogs.com/xiaoguanqiu/archive/2013/02/26/2933451.html
總結
以上是生活随笔為你收集整理的Windows API 逐个逐个学MessageBox(5) 遍历驱动器并获取驱动器属性 GetLogicalDriveStrings、GetDriveTypeA...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL学习笔记——显示数据库信息
- 下一篇: 微软的Surface平板电脑RT版,RT