使用C++对磁盘进行检测获取磁盘相应的信息_第1页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、使用c+对磁盘进行检测,获取磁盘相应的信息tern "c" _decpec(dll)int getlogicdiskinfo() int dtype; int si = 0; bool relt; unsign _int64 i64freebytestocaller; unsigned _int64 i64totalbytes; unsigned _int64 i64freebytes; float totalsize;/总空间 float usablesize;/可用空间 int dslength = getlogicaldrivesings(0,null);/通过该函数

2、猎取全部驱动器字符串信息长度 cout "长度为:" dslength endl; ofstream in;/向文件写内容 in.open("d:diskinfo.txt",ios:tru);/ios:trunc表示在打开文件前将文件清空,因为是写入操作,当文件不存在则创建 char* dstr = new chardslength; getlogicaldrivestrings(dslength,(lptstr)dstr); for(int i=0;i dslength/4;+i) char dir3 = dstrsi,':','

3、;' /cout "磁盘名称为:" dir0 dir1 dir2 endl; in "磁盘名称为:" dir0 dir1 dir2 "n" char str3 = 0,0,0; str0 = dir0; str1 = dir1; string dirname = str; dtype = getdrivetype(lptstr)dstr+i*4);/通过该函数猎取磁盘的类型 string drivertype; if(dtype = drive_fixed) drivertype = "本地磁盘" /cou

4、t 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 "磁盘类型为:"

5、; 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(d

6、type = 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) i64fr

7、eebytes);/猎取磁盘的空间状态 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 "总空间为:" tota

8、lsize "gb" "n" in "可用空间为:" usablesize "gb" "n" in "=" "n" else /cout "未检测到磁盘设备" endl; in "未检测到磁盘设备" "n" else if(dirname = "d:")/当磁盘为d盘时 result = getdiskfreespaceex(_t("d:"),(pularg

9、e_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 "可用空间为:&qu

10、ot; usablesize "gb" endl; /cout "=" endl; in "总空间为:" totalsize "gb" "n" in "可用空间为:" usablesize "gb" "n" in "=" "n" else /cout "未检测到磁盘设备" endl; in "未检测到磁盘设备" "n" else if(d

11、irname = "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/

12、1024/1024; /cout "总空间为:" totalsize "gb" endl; /cout "可用空间为:" usablesize "gb" endl; /cout "=" endl; in "总空间为:" totalsize "gb" "n" in "可用空间为:" usablesize "gb" "n" in "=" "n"

13、; 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) totalsiz

14、e = (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 &

15、quot;可用空间为:" usablesize "gb" "n" in "=" "n" else /cout "未检测到磁盘设备" endl; in "未检测到磁盘设备" "n" else if(dirname = "g:")/当磁盘为e盘时 result = getdiskfreespaceex(_t("g:"),(pularge_integer) i64freebytestocaller,(pularg

16、e_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; /co

17、ut "=" endl; in "总空间为:" totalsize "gb" "n" in "可用空间为:" usablesize "gb" "n" in "=" "n" else /cout "未检测到磁盘设备" endl; in "未检测到磁盘设备" "n" si+=8; in.close();/关闭文件 if(null != dtype) return

18、 dtype; else return -1; 该代码主要用来猎取磁盘的盘符、磁盘的类型、磁盘的总空间大小以及磁盘的可用空间大小。 public ic map string, returnpo getdiscinfo() string dirname = "d:disk" string name = "info.txt" string disknamestr = "磁盘名称" string disktypestr = "磁盘类型" string totalsizestr = "总空间" stri

19、ng 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;

20、 try jnative = new jnative("pcsuite", "getlogicdiskinfo");/ 加载pcsuite.dll文件 jnative.retval(type.int); / jnative.invoke(); file file = new file(dirname + filename);/ 实例化文件对象,读取一个放置于本地磁盘中存放磁盘信息的文件 if (file.exists() bufferedreader br = new bufferedreader(new filereader(file); string

21、 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);/ 猎取

22、盘符名称 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)/ 当磁盘类型为可移动磁盘时 us

23、bpo.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 =

24、 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.a(usbp

25、o);/ 将磁盘信息添加一个列表中 else count+; number+; system.out.print("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 ing this file"); ch (nativeexception e) e.printstacktrace(); returnpo.setexcept(e); returnpo.setresultflag(false); r

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论