学习API使用GetLogicalDriveString获取驱动器根路径

```c

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#define BUFSIZE 1024

BOOL GetDirverInfo(LPSTR szDrive);

void main(void)
{
CHAR szLogicalDriveStrings[BUFSIZE];
PCHAR szDrive;
/*****************************************
* ZeroMemory(szLogicalDriveStrings,BUFSIZE)
* 功能:将指定内存块至零
* 参数:szLogicalDriveStrings,BUFSIZE
* szLogicalDriveStrings 指向一块准备用0来填充的内存区域的开始地址
* BUFSIZE 准备用0来填充的内存区域的大小,按字节来计算
* 返回值:无
******************************************/
ZeroMemory(szLogicalDriveStrings,BUFSIZE);
//获取逻辑驱动器卷名
GetLogicalDriveStrings(BUFSIZE - 1,szLogicalDriveStrings);
szDrive = (PCHAR)szLogicalDriveStrings;
//循环处理每个卷
do
{
if (!GetDirverInfo(szDrive))
{
printf("\nGet Volume Information Error: %d",GetLastError());
}
szDrive += (lstrlen(szDrive)+1);
} while (*szDrive != '\x00');
char ch;
ch = getchar();
}

/*********************************************
* BOOL GetDirverInfo(LPSTR szDrive)
* 功能:获取驱动器的属性
* 参数:LPSTR szDrive
* 指明要获取属性的驱动器的根目录,如C:\
* 返回值 BOOL:表示是否成功
*********************************************/
BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDiveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
CHAR szFileSystemNameBuffer[BUFSIZE];
CHAR szDirverName[MAX_PATH];
printf("\n%s\n",szDrive);
/*********************
* UINT WINAPI GetDriveType(LPSTR szDrive);
* 功能:判断一个磁盘驱动器的类型
* 参数:LPSTR szDrive
* 指明要获取属性的驱动器的根目录,如C:\
* 返回值 UINT:表示磁盘类型
**********************/
uDiveType = GetDriveType(szDrive);
switch(uDiveType)
{
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://RAM
printf("The drive is a RAM disk.");
break;
default:
break;
}
/***********************************
*GetVolumeInformation(szDrive,szDirverName,MAX_PATH,
&dwVolumeSerialNumber,&dwMaximumComponentLength,
&dwFileSystemFlags,szFileSystemNameBuffer,BUFSIZE)
* 功能:获取磁盘驱动器的信息
* 参数:如下
* 返回值:非零即成功
***********************************/
if (!GetVolumeInformation(szDrive,
szDirverName,//卷标(字符串)
MAX_PATH,//szDirveName的长度
&dwVolumeSerialNumber,//用于装载磁盘卷序列号的变量
&dwMaximumComponentLength,//用于装载文件名每一部分的长度
&dwFileSystemFlags,//用于装载一个或多个二进制位标志的变量
szFileSystemNameBuffer,//系统类型
BUFSIZE))
{
return FALSE;
}
if (0!=lstrlen(szDirverName))
{
printf("\nDrive Name is %s\n",szDirverName);
}
printf("\nVolume Serial Number is %u",dwVolumeSerialNumber);
printf("\nMaximun 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");
}
printf("...\n");
return TRUE;
}

```

转载于:https://www.cnblogs.com/zjf94/p/5456255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值