HardDisk类添加异常捕获

croire 3 years ago
parent 6c8a738e02
commit f93306509d

@ -1,4 +1,6 @@
using System.IO; using DownKyi.Core.Logging;
using System;
using System.IO;
namespace DownKyi.Core.Utils namespace DownKyi.Core.Utils
{ {
@ -11,17 +13,26 @@ namespace DownKyi.Core.Utils
/// <returns></returns> /// <returns></returns>
public static long GetHardDiskSpace(string hardDiskName) public static long GetHardDiskSpace(string hardDiskName)
{ {
long totalSize = new long(); long totalSize = 0;
hardDiskName = $"{hardDiskName}:\\";
DriveInfo[] drives = DriveInfo.GetDrives();
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; return totalSize;
} }
@ -33,17 +44,25 @@ namespace DownKyi.Core.Utils
/// <returns></returns> /// <returns></returns>
public static long GetHardDiskFreeSpace(string hardDiskName) public static long GetHardDiskFreeSpace(string hardDiskName)
{ {
long freeSpace = new long(); long freeSpace = 0;
hardDiskName = $"{hardDiskName}:\\"; try
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{ {
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; return freeSpace;
} }

Loading…
Cancel
Save