硬盘爱好者 发表于 2012-4-20 08:46:49

使用C++对磁盘进行检测,获取磁盘相应的信息

extern "C" _declspec(dllexport)int getLogicDiskInfo()
{
int DType;
int si = 0;

BOOL result;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;

float totalSize;//总空间
float usableSize;//可用空间

int DSLength = GetLogicalDriveStrings(0,NULL);//通过该函数获取所有驱动器字符串信息长度
cout<<"长度为:"<<DSLength<<endl;


ofstream in;//向文件写内容
in.open("D:\\disk\\info.txt",ios::trunc);//ios::trunc表示在打开文件前将文件清空,由于是写入操作,当文件不存在则创建

char* DStr = new char;
GetLogicalDriveStrings(DSLength,(LPTSTR)DStr);
for(int i=0;i<DSLength/4;++i)
{
char dir = {DStr,':','\\'};
//cout<<"磁盘名称为:"<<dir<<dir<<dir<<endl;
in<<"磁盘名称为:"<<dir<<dir<<dir<<"\n";

char str = {0,0,0};
str = dir;
str = dir;

string dirName = str;

DType = GetDriveType((LPTSTR)DStr+i*4);//通过该函数获取磁盘的类型
string driverType;

if(DType == DRIVE_FIXED)
{
driverType = "本地磁盘";
//cout<<driverType<<endl;
in<<"磁盘类型为:"<<driverType<<"\n";
}
else if(DType == DRIVE_CDROM)
{
driverType = "光驱";
//cout<<driverType<<endl;
in<<"磁盘类型为:"<<driverType<<"\n";
}
else if(DType == DRIVE_REMOVABLE)
{
driverType = "可移动磁盘";
//cout<<driverType<<endl;
in<<"磁盘类型为:"<<driverType<<"\n";
}
else if(DType == DRIVE_REMOTE)
{
driverType = "网络磁盘";
//cout<<driverType<<endl;
in<<"磁盘类型为:"<<driverType<<"\n";
}
else if(DType == DRIVE_RAMDISK)
{
driverType = "虚拟RAM磁盘";
//cout<<driverType<<endl;
in<<"磁盘类型为:"<<driverType<<"\n";
}
else if(DType == DRIVE_UNKNOWN)
{
driverType = "未知设备";
//cout<<driverType<<endl;
in<<"磁盘类型为:"<<driverType<<"\n";
}

if(dirName == "C:")//当磁盘为C盘时
{
result = GetDiskFreeSpaceEx(_T("C:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
if(result)
{
totalSize = (float)i64TotalBytes/1024/1024/1024;
usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
//cout<<"总空间为:"<<totalSize<<"GB"<<endl;
//cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
//cout<<"=============================================================="<<endl;

in<<"总空间为:"<<totalSize<<"GB"<<"\n";
in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
in<<"=============================================================="<<"\n";
}
else
{
//cout<<"未检测到磁盘设备"<<endl;
in<<"未检测到磁盘设备"<<"\n";
}
}
else if(dirName == "D:")//当磁盘为D盘时
{
result = GetDiskFreeSpaceEx(_T("D:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
if(result)
{
totalSize = (float)i64TotalBytes/1024/1024/1024;
usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
//cout<<"总空间为:"<<totalSize<<"GB"<<endl;
//cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
//cout<<"=============================================================="<<endl;

in<<"总空间为:"<<totalSize<<"GB"<<"\n";
in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
in<<"=============================================================="<<"\n";
}
else
{
//cout<<"未检测到磁盘设备"<<endl;
in<<"未检测到磁盘设备"<<"\n";
}
}
else if(dirName == "E:")//当磁盘为E盘时
{
result = GetDiskFreeSpaceEx(_T("E:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
if(result)
{
totalSize = (float)i64TotalBytes/1024/1024/1024;
usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
//cout<<"总空间为:"<<totalSize<<"GB"<<endl;
//cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
//cout<<"=============================================================="<<endl;

in<<"总空间为:"<<totalSize<<"GB"<<"\n";
in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
in<<"=============================================================="<<"\n";
}
else
{
//cout<<"未检测到磁盘设备"<<endl;
in<<"未检测到磁盘设备"<<"\n";
}
}
else if(dirName == "F:")//当磁盘为F盘时
{
result = GetDiskFreeSpaceEx(_T("F:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
if(result)
{
totalSize = (float)i64TotalBytes/1024/1024/1024;
usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
//cout<<"总空间为:"<<totalSize<<"GB"<<endl;
//cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
//cout<<"=============================================================="<<endl;

in<<"总空间为:"<<totalSize<<"GB"<<"\n";
in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
in<<"=============================================================="<<"\n";
}
else
{
//cout<<"未检测到磁盘设备"<<endl;
in<<"未检测到磁盘设备"<<"\n";
}
}
else if(dirName == "G:")//当磁盘为E盘时
{
result = GetDiskFreeSpaceEx(_T("G:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
if(result)
{
totalSize = (float)i64TotalBytes/1024/1024/1024;
usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
//cout<<"总空间为:"<<totalSize<<"GB"<<endl;
//cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
//cout<<"=============================================================="<<endl;

in<<"总空间为:"<<totalSize<<"GB"<<"\n";
in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
in<<"=============================================================="<<"\n";
}
else
{
//cout<<"未检测到磁盘设备"<<endl;
in<<"未检测到磁盘设备"<<"\n";
}
}

si+=8;
}

in.close();//关闭文件

if(NULL != DType)
{
return DType;
}
else
{
return -1;
}
}


该代码主要用来获取磁盘的盘符、磁盘的类型、磁盘的总空间大小以及磁盘的可用空间大小。


public static Map<String, ReturnPo> getDiscInfo() {

String dirName = "D:\\disk\\";
String fileName = "info.txt";

String diskNameStr = "磁盘名称";
String diskTypeStr = "磁盘类型";
String totalSizeStr = "总空间";
String usableSizeStr = "可用空间";

String diskName = null;
String diskType = null;

float totalSize = 0;
float usableSize = 0;

Map<String, ReturnPo> map = new HashMap<String, ReturnPo>();
ReturnPo returnPo = new ReturnPo();
List<UsbPo> list = new ArrayList<UsbPo>();
UsbPo usbPo = null;

JNative jNative = null;

try {

jNative = new JNative("PCsuite", "getLogicDiskInfo");// 加载PCsuite.dll文件

jNative.setRetVal(Type.INT);
// jNative.invoke();

File file = new File(dirName + fileName);// 实例化文件对象,读取一个放置于本地磁盘中存放磁盘信息的文件
if (file.exists()) {

BufferedReader br = new BufferedReader(new FileReader(file));

String line = null;
int count = 0;
int number = 0;

for (int i = 0; i <= count; i++) {

while ((line = br.readLine()) != null) {

if (number % 5 == 0) {
usbPo = new UsbPo();// 实例化磁盘对象,用来存放磁盘信息
}

if (!"=============================================================="
.equals(line)) {

if (line.contains(diskNameStr)) {

diskName = line
.substring(line.indexOf(":") + 1);// 获取盘符名称
usbPo.setName(diskName);// 设置盘符名称
number++;

} else if (line.contains(diskTypeStr)) {

diskType = line
.substring(line.indexOf(":") + 1);// 获取磁盘类型
if (diskType.equals(localDiscName))// 当磁盘类型为本地磁盘时
{

usbPo.setFlag(0);// 当磁盘类型为本地磁盘时,设置为0
number++;

} else if (diskType.equals(moveDiscName))// 当磁盘类型为可移动磁盘时
{

usbPo.setFlag(1);// 当磁盘类型为可移动磁盘时,设置为1
number++;

}

} else if (line.contains(totalSizeStr)) {

totalSize = Float.parseFloat(line.substring(
line.indexOf(":") + 1, line
.indexOf("GB")));// 获取磁盘的总空间
usbPo.setTotalSize(totalSize);// 设置磁盘的总空间
number++;

} else if (line.contains(usableSizeStr)) {

usableSize = Float.parseFloat(line.substring(
line.indexOf(":") + 1, line
.indexOf("GB")));// 获取磁盘的可用空间
usbPo.setUsableSize(usableSize);// 设置磁盘的可用空间
number++;

}

if (null != usbPo && null != usbPo.getName()
&& !"".equals(usbPo.getName())
&& usbPo.getTotalSize() > 0
&& usbPo.getUsableSize() > 0) {

list.add(usbPo);// 将磁盘信息添加一个列表中

}

} else {

count++;
number++;
System.out.println("count is:" + count);

}

}

}

returnPo.setExcept(null);
returnPo.setResultFlag(true);
returnPo.setResultInfo(list);
returnPo.setMethodId(InvokeMethod.methodId1);

map.put(SocketServer.resultInfo, returnPo);

} else {

logger.error("not finding this file……");

}

} catch (NativeException e) {

e.printStackTrace();
returnPo.setExcept(e);
returnPo.setResultFlag(false);
returnPo.setResultInfo(e.getMessage());
logger.error(e);

map.put(SocketServer.resultInfo, returnPo);
return map;

} catch (IllegalAccessException e) {

e.printStackTrace();
returnPo.setExcept(e);
returnPo.setResultFlag(false);
returnPo.setResultInfo(e.getMessage());
logger.error(e);

map.put(SocketServer.resultInfo, returnPo);
return map;

} catch (FileNotFoundException e) {

e.printStackTrace();
returnPo.setExcept(e);
returnPo.setResultFlag(false);
returnPo.setResultInfo(e.getMessage());
logger.error(e);

map.put(SocketServer.resultInfo, returnPo);
return map;

} catch (IOException e) {

e.printStackTrace();
returnPo.setExcept(e);
returnPo.setResultFlag(false);
returnPo.setResultInfo(e.getMessage());
logger.error(e);

map.put(SocketServer.resultInfo, returnPo);
return map;

}

return map;

}

做法概要:通过使用C++代码,编译生成相应的dll文件,然后通过JNative去调用生成的dll文件,并调取相应的方法进行检测。
(http://bbs.intohard.com/thread-113097-1-1.html)

ikkyphoenix 发表于 2013-7-9 17:49:31

支持一下,多谢分享!

五爱街 发表于 2014-3-10 10:22:25


支持一下,多谢分享!

奇彩科技 发表于 2014-3-15 15:16:45

有那么点高深

yjip520 发表于 2014-5-17 15:15:40

不支持linux分区吧。linux分区怎么读取出来在WINDOWS下面用C++

丁刚 发表于 2014-10-23 21:37:00

支持一下,学习了啊
页: [1]
查看完整版本: 使用C++对磁盘进行检测,获取磁盘相应的信息