| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using HXX.Scanner.Common;
- namespace HXX.Scanner.Biz
- {
- /// <summary>
- /// 磁盘相关业务
- /// </summary>
- public class biz_disk
- {
- /// <summary>
- /// 获取磁盘剩余空间
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string getDataDirLeftSpace(string path)
- {
- string result = string.Empty;
- try
- {
- if (path != null && path.Length > 2)
- {
- var s = GetHardDiskFreeSpace(path.Substring(0, 1));
- if (s > 10)
- {
- result = (int)s + "G";
- }
- else if (s < 1)
- {
- result = (int)(s * 1000) + "M";
- }
- else
- {
- result = s.ToString("0.0") + "G";
- }
- }
- }
- catch (Exception ee)
- {
- result = "--G";
- LogManager.WriteLog(path + Environment.NewLine + ee.Message);
- }
- return result;
- }
- /// <summary>
- /// 获取磁盘剩余空间
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static double getDataDirLeftSpace_double(string path)
- {
- return GetHardDiskFreeSpace(path.Substring(0, 1));
- }
- /// <summary>
- /// 获取指定驱动器的剩余空间总大小(单位为GB)
- /// </summary>
- /// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param>
- private static double GetHardDiskFreeSpace(string str_HardDiskName)
- {
- double freeSpace = 0;
- str_HardDiskName = str_HardDiskName + ":\\";
- foreach (var drive in DriveInfo.GetDrives())
- {
- if (drive.Name == str_HardDiskName.ToUpper())
- {
- freeSpace = drive.TotalFreeSpace * 1.0 / (1024 * 1024 * 1024);
- break;
- }
- }
- return freeSpace;
- }
- /// <summary>
- /// 获取当前磁盘使用状态
- /// </summary>
- /// <returns></returns>
- public static ResponseEntity check_disk_status()
- {
- ResponseEntity result = new ResponseEntity();
- try
- {
- var dir = config_manager.Get("dataDir");
- if (string.IsNullOrEmpty(dir))
- {
- result.Status = 520;
- result.Message = "请先设置数据存放位置";
- }
- else
- {
- var vol = getDataDirLeftSpace_double(dir);
- if (vol < 0.5)
- {
- result.Status = 509;
- result.Message = "磁盘剩余空间较少";
- }
- else if (vol < 5)
- {
- result.Status = 510;
- result.Message = "磁盘剩余空间较少";
- }
- else
- {
- result.Status = 200;
- result.Message = "ok";
- }
- }
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- result.Status = 509;
- result.Message = ee.Message;
- }
- return result;
- }
- }
- }
|