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

[复制链接]
  • TA的每日心情
    开心
    2014-4-2 21:30
  • 签到天数: 3 天

    [LV.2]九品芝麻官

    534

    主题

    400

    回帖

    7742

    积分

    Intohard Team

    Rank: 7Rank: 7Rank: 7

    积分
    7742
    发表于 2012-4-20 08:46:49 | 显示全部楼层 |阅读模式
    1. extern "C" _declspec(dllexport)int getLogicDiskInfo()
    2. {
    3. int DType;
    4. int si = 0;

    5. BOOL result;
    6. unsigned _int64 i64FreeBytesToCaller;
    7. unsigned _int64 i64TotalBytes;
    8. unsigned _int64 i64FreeBytes;

    9. float totalSize;//总空间
    10. float usableSize;//可用空间

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


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

    15. char* DStr = new char[DSLength];
    16. GetLogicalDriveStrings(DSLength,(LPTSTR)DStr);
    17. for(int i=0;i<DSLength/4;++i)
    18. {
    19. char dir[3] = {DStr[si],':','\\'};
    20. //cout<<"磁盘名称为:"<<dir[0]<<dir[1]<<dir[2]<<endl;
    21. in<<"磁盘名称为:"<<dir[0]<<dir[1]<<dir[2]<<"\n";

    22. char str[3] = {0,0,0};
    23. str[0] = dir[0];
    24. str[1] = dir[1];

    25. string dirName = str;

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

    28. if(DType == DRIVE_FIXED)
    29. {
    30. driverType = "本地磁盘";
    31. //cout<<driverType<<endl;
    32. in<<"磁盘类型为:"<<driverType<<"\n";
    33. }
    34. else if(DType == DRIVE_CDROM)
    35. {
    36. driverType = "光驱";
    37. //cout<<driverType<<endl;
    38. in<<"磁盘类型为:"<<driverType<<"\n";
    39. }
    40. else if(DType == DRIVE_REMOVABLE)
    41. {
    42. driverType = "可移动磁盘";
    43. //cout<<driverType<<endl;
    44. in<<"磁盘类型为:"<<driverType<<"\n";
    45. }
    46. else if(DType == DRIVE_REMOTE)
    47. {
    48. driverType = "网络磁盘";
    49. //cout<<driverType<<endl;
    50. in<<"磁盘类型为:"<<driverType<<"\n";
    51. }
    52. else if(DType == DRIVE_RAMDISK)
    53. {
    54. driverType = "虚拟RAM磁盘";
    55. //cout<<driverType<<endl;
    56. in<<"磁盘类型为:"<<driverType<<"\n";
    57. }
    58. else if(DType == DRIVE_UNKNOWN)
    59. {
    60. driverType = "未知设备";
    61. //cout<<driverType<<endl;
    62. in<<"磁盘类型为:"<<driverType<<"\n";
    63. }

    64. if(dirName == "C:")//当磁盘为C盘时
    65. {
    66. result = GetDiskFreeSpaceEx(_T("C:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
    67. if(result)
    68. {
    69. totalSize = (float)i64TotalBytes/1024/1024/1024;
    70. usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
    71. //cout<<"总空间为:"<<totalSize<<"GB"<<endl;
    72. //cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
    73. //cout<<"=============================================================="<<endl;

    74. in<<"总空间为:"<<totalSize<<"GB"<<"\n";
    75. in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
    76. in<<"=============================================================="<<"\n";
    77. }
    78. else
    79. {
    80. //cout<<"未检测到磁盘设备"<<endl;
    81. in<<"未检测到磁盘设备"<<"\n";
    82. }
    83. }
    84. else if(dirName == "D:")//当磁盘为D盘时
    85. {
    86. result = GetDiskFreeSpaceEx(_T("D:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
    87. if(result)
    88. {
    89. totalSize = (float)i64TotalBytes/1024/1024/1024;
    90. usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
    91. //cout<<"总空间为:"<<totalSize<<"GB"<<endl;
    92. //cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
    93. //cout<<"=============================================================="<<endl;

    94. in<<"总空间为:"<<totalSize<<"GB"<<"\n";
    95. in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
    96. in<<"=============================================================="<<"\n";
    97. }
    98. else
    99. {
    100. //cout<<"未检测到磁盘设备"<<endl;
    101. in<<"未检测到磁盘设备"<<"\n";
    102. }
    103. }
    104. else if(dirName == "E:")//当磁盘为E盘时
    105. {
    106. result = GetDiskFreeSpaceEx(_T("E:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
    107. if(result)
    108. {
    109. totalSize = (float)i64TotalBytes/1024/1024/1024;
    110. usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
    111. //cout<<"总空间为:"<<totalSize<<"GB"<<endl;
    112. //cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
    113. //cout<<"=============================================================="<<endl;

    114. in<<"总空间为:"<<totalSize<<"GB"<<"\n";
    115. in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
    116. in<<"=============================================================="<<"\n";
    117. }
    118. else
    119. {
    120. //cout<<"未检测到磁盘设备"<<endl;
    121. in<<"未检测到磁盘设备"<<"\n";
    122. }
    123. }
    124. else if(dirName == "F:")//当磁盘为F盘时
    125. {
    126. result = GetDiskFreeSpaceEx(_T("F:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
    127. if(result)
    128. {
    129. totalSize = (float)i64TotalBytes/1024/1024/1024;
    130. usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
    131. //cout<<"总空间为:"<<totalSize<<"GB"<<endl;
    132. //cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
    133. //cout<<"=============================================================="<<endl;

    134. in<<"总空间为:"<<totalSize<<"GB"<<"\n";
    135. in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
    136. in<<"=============================================================="<<"\n";
    137. }
    138. else
    139. {
    140. //cout<<"未检测到磁盘设备"<<endl;
    141. in<<"未检测到磁盘设备"<<"\n";
    142. }
    143. }
    144. else if(dirName == "G:")//当磁盘为E盘时
    145. {
    146. result = GetDiskFreeSpaceEx(_T("G:"),(PULARGE_INTEGER)&i64FreeBytesToCaller,(PULARGE_INTEGER)&i64TotalBytes,(PULARGE_INTEGER)&i64FreeBytes);//获取磁盘的空间状态
    147. if(result)
    148. {
    149. totalSize = (float)i64TotalBytes/1024/1024/1024;
    150. usableSize = (float)i64FreeBytesToCaller/1024/1024/1024;
    151. //cout<<"总空间为:"<<totalSize<<"GB"<<endl;
    152. //cout<<"可用空间为:"<<usableSize<<"GB"<<endl;
    153. //cout<<"=============================================================="<<endl;

    154. in<<"总空间为:"<<totalSize<<"GB"<<"\n";
    155. in<<"可用空间为:"<<usableSize<<"GB"<<"\n";
    156. in<<"=============================================================="<<"\n";
    157. }
    158. else
    159. {
    160. //cout<<"未检测到磁盘设备"<<endl;
    161. in<<"未检测到磁盘设备"<<"\n";
    162. }
    163. }

    164. si+=8;
    165. }

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

    167. if(NULL != DType)
    168. {
    169. return DType;
    170. }
    171. else
    172. {
    173. return -1;
    174. }
    175. }


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


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

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

    180. String diskNameStr = "磁盘名称";
    181. String diskTypeStr = "磁盘类型";
    182. String totalSizeStr = "总空间";
    183. String usableSizeStr = "可用空间";

    184. String diskName = null;
    185. String diskType = null;

    186. float totalSize = 0;
    187. float usableSize = 0;

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

    192. JNative jNative = null;

    193. try {

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

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

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

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

    200. String line = null;
    201. int count = 0;
    202. int number = 0;

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

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

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

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

    210. if (line.contains(diskNameStr)) {

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

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

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

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

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

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

    226. }

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

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

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

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

    239. }

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

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

    245. }

    246. } else {

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

    250. }

    251. }

    252. }

    253. returnPo.setExcept(null);
    254. returnPo.setResultFlag(true);
    255. returnPo.setResultInfo(list);
    256. returnPo.setMethodId(InvokeMethod.methodId1);

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

    258. } else {

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

    260. }

    261. } catch (NativeException e) {

    262. e.printStackTrace();
    263. returnPo.setExcept(e);
    264. returnPo.setResultFlag(false);
    265. returnPo.setResultInfo(e.getMessage());
    266. logger.error(e);

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

    269. } catch (IllegalAccessException e) {

    270. e.printStackTrace();
    271. returnPo.setExcept(e);
    272. returnPo.setResultFlag(false);
    273. returnPo.setResultInfo(e.getMessage());
    274. logger.error(e);

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

    277. } catch (FileNotFoundException e) {

    278. e.printStackTrace();
    279. returnPo.setExcept(e);
    280. returnPo.setResultFlag(false);
    281. returnPo.setResultInfo(e.getMessage());
    282. logger.error(e);

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

    285. } catch (IOException e) {

    286. e.printStackTrace();
    287. returnPo.setExcept(e);
    288. returnPo.setResultFlag(false);
    289. returnPo.setResultInfo(e.getMessage());
    290. logger.error(e);

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

    293. }

    294. return map;

    295. }

    复制代码
    做法概要:通过使用C++代码,编译生成相应的dll文件,然后通过JNative去调用生成的dll文件,并调取相应的方法进行检测。
    (http://bbs.intohard.com/thread-113097-1-1.html)
  • TA的每日心情
    奋斗
    2013-10-23 16:36
  • 签到天数: 130 天

    [LV.7]四品道员

    2

    主题

    514

    回帖

    1004

    积分

    [INTOHARD]营长

    Rank: 6Rank: 6

    积分
    1004
    发表于 2013-7-9 17:49:31 | 显示全部楼层
    支持一下,多谢分享!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2016-4-12 06:22
  • 签到天数: 80 天

    [LV.6]五品郎中

    0

    主题

    279

    回帖

    580

    积分

    [INTOHARD]连长

    Rank: 4

    积分
    580
    发表于 2014-3-10 10:22:25 | 显示全部楼层

    支持一下,多谢分享!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-3-24 16:38
  • 签到天数: 27 天

    [LV.4]七品知县

    1

    主题

    51

    回帖

    124

    积分

    [INTOHARD]班长

    Rank: 2

    积分
    124
    发表于 2014-3-15 15:16:45 | 显示全部楼层
    有那么点高深
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2015-5-8 09:34
  • 签到天数: 156 天

    [LV.7]四品道员

    12

    主题

    186

    回帖

    880

    积分

    [INTOHARD]连长

    Rank: 4

    积分
    880
    发表于 2014-5-17 15:15:40 | 显示全部楼层
    不支持linux分区吧。linux分区怎么读取出来在WINDOWS下面用C++
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    4 小时前
  • 签到天数: 3723 天

    [LV.Master]三朝元老

    38

    主题

    6145

    回帖

    2万

    积分

    [INTOHARD]旅长

    Rank: 9Rank: 9Rank: 9

    积分
    21785
    发表于 2014-10-23 21:37:00 | 显示全部楼层
    支持一下,学习了啊
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    快速回复 返回顶部 返回列表