From f93306509d30b97f9667fdfd5510f99f2bfc3008 Mon Sep 17 00:00:00 2001 From: croire <1432593898@qq.com> Date: Sat, 12 Mar 2022 01:50:53 +0800 Subject: [PATCH] =?UTF-8?q?HardDisk=E7=B1=BB=E6=B7=BB=E5=8A=A0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DownKyi.Core/Utils/HardDisk.cs | 47 ++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/DownKyi.Core/Utils/HardDisk.cs b/DownKyi.Core/Utils/HardDisk.cs index 046384d..9427ad1 100644 --- a/DownKyi.Core/Utils/HardDisk.cs +++ b/DownKyi.Core/Utils/HardDisk.cs @@ -1,4 +1,6 @@ -using System.IO; +using DownKyi.Core.Logging; +using System; +using System.IO; namespace DownKyi.Core.Utils { @@ -11,17 +13,26 @@ namespace DownKyi.Core.Utils /// public static long GetHardDiskSpace(string hardDiskName) { - long totalSize = new long(); - hardDiskName = $"{hardDiskName}:\\"; - DriveInfo[] drives = DriveInfo.GetDrives(); + long totalSize = 0; - foreach (DriveInfo drive in drives) + try { - if (drive.Name == hardDiskName) + hardDiskName = $"{hardDiskName}:\\"; + DriveInfo[] drives = DriveInfo.GetDrives(); + + foreach (DriveInfo drive in drives) { - totalSize = drive.TotalSize; + if (drive.Name == hardDiskName) + { + totalSize = drive.TotalSize; + } } } + catch (Exception e) + { + Debugging.Console.PrintLine("GetHardDiskSpace()发生异常: {0}", e); + LogManager.Error("HardDisk", e); + } return totalSize; } @@ -33,17 +44,25 @@ namespace DownKyi.Core.Utils /// public static long GetHardDiskFreeSpace(string hardDiskName) { - long freeSpace = new long(); - hardDiskName = $"{hardDiskName}:\\"; - DriveInfo[] drives = DriveInfo.GetDrives(); - - foreach (DriveInfo drive in drives) + long freeSpace = 0; + try { - if (drive.Name == hardDiskName) + hardDiskName = $"{hardDiskName}:\\"; + DriveInfo[] drives = DriveInfo.GetDrives(); + + foreach (DriveInfo drive in drives) { - freeSpace = drive.TotalFreeSpace; + if (drive.Name == hardDiskName) + { + freeSpace = drive.TotalFreeSpace; + } } } + catch (Exception e) + { + Debugging.Console.PrintLine("GetHardDiskFreeSpace()发生异常: {0}", e); + LogManager.Error("HardDisk", e); + } return freeSpace; }