应用程序经常要对磁盘做操作,如拷贝文件,读取文件,列举磁盘之类的。

在读取文件时会遇到有些移动盘被写保护的情况,通常处理的方式是在对应盘里写个文件判断是否成功,为了跳过系统弹框用SetErrorMode设置一下

如下代码:

#include <io.h>
#include <stdio.h>
BOOL IsDeviceProtected(char Drive)
{
	BOOL rst = true; 
	char buf[] = "?:\\*.*";
	buf[0] = Drive;
	_finddata_t fd; 
	intptr_t pf = _findfirst(buf, &fd);
	if (pf != -1)
	{  
		do
		{
			if (fd.time_access == -1 || fd.time_create == -1 || fd.time_write == -1) //校验时间属性  还可以校验文件属性
			{
				rst = false;
				break;
			}
			printf("%s\n", fd.name);
		} while (!_findnext(pf, &fd));
		_findclose(pf);
	}
	else
		rst = false; 
	return rst;
}