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,7 +13,10 @@ 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;
try
{
hardDiskName = $"{hardDiskName}:\\"; hardDiskName = $"{hardDiskName}:\\";
DriveInfo[] drives = DriveInfo.GetDrives(); DriveInfo[] drives = DriveInfo.GetDrives();
@ -22,6 +27,12 @@ namespace DownKyi.Core.Utils
totalSize = drive.TotalSize; totalSize = drive.TotalSize;
} }
} }
}
catch (Exception e)
{
Debugging.Console.PrintLine("GetHardDiskSpace()发生异常: {0}", e);
LogManager.Error("HardDisk", e);
}
return totalSize; return totalSize;
} }
@ -33,7 +44,9 @@ 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;
try
{
hardDiskName = $"{hardDiskName}:\\"; hardDiskName = $"{hardDiskName}:\\";
DriveInfo[] drives = DriveInfo.GetDrives(); DriveInfo[] drives = DriveInfo.GetDrives();
@ -44,6 +57,12 @@ namespace DownKyi.Core.Utils
freeSpace = drive.TotalFreeSpace; freeSpace = drive.TotalFreeSpace;
} }
} }
}
catch (Exception e)
{
Debugging.Console.PrintLine("GetHardDiskFreeSpace()发生异常: {0}", e);
LogManager.Error("HardDisk", e);
}
return freeSpace; return freeSpace;
} }

Loading…
Cancel
Save