c++对于磁盘有如下宏定义
#define DRIVE_UNKNOWN 0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE 2
#define DRIVE_FIXED 3
#define DRIVE_REMOTE 4
#define DRIVE_CDROM 5
#define DRIVE_RAMDISK 6
#define DRIVE_FIXED 3 这个就是本地磁盘的宏
所以是不是本地磁盘我们只需要获取磁盘的属性进行一下比较就可以了
代码如下:
#include "stdafx.h"
#include <windows.h>
#include <string>
bool IsDiskWriteable( LPCTSTR lpPath)
{
std::wstring sPath = lpPath;
int nPos = sPath.find_first_of( _T( "\\" ) );
sPath = sPath.substr(0, nPos);
UINT nRes = ::GetDriveType( sPath.c_str() );
if ( DRIVE_FIXED == nRes )
{
return true;
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
bool bRet = IsDiskWriteable( _T("F:\\") );
return 0;
}
如有错误请指正
本文介绍了一个使用C++编写的简单示例程序,该程序通过Windows API函数GetDriveType来判断指定路径是否为本地固定磁盘。具体实现包括解析路径、调用API获取磁盘类型并进行判断。
368

被折叠的 条评论
为什么被折叠?



