[C#] 监视所在的计算机的磁盘使用率是否超过设定百分率 →→→→→进入此内容的聊天室

来自 , 2019-04-09, 写在 C#, 查看 137 次.
URL http://www.code666.cn/view/5ac8bb8a
  1. using System.IO;
  2. using System.Net;
  3. using System.Net.Mail;
  4.  
  5. namespace MonitorDiskspace
  6. {
  7.     class Program
  8.     {
  9.         private static bool CheckTotalFreeSpaceRate(string driveName)
  10.         {
  11.             foreach (DriveInfo drive in DriveInfo.GetDrives())
  12.             {
  13.                 if (drive.IsReady && drive.Name == driveName)
  14.                     return (drive.TotalFreeSpace * 100 / drive.TotalSize) < 15;
  15.             }
  16.             return false;
  17.         }
  18.  
  19.         private static void SendEmail(string drive)
  20.         {
  21.             MailMessage message = new MailMessage();
  22.             message.To.Add("AppSupport@company.com");
  23.             message.Subject = "Alert!!! Disk Almost full";
  24.             message.From = new MailAddress("AppSupport@company.com");
  25.             message.Body = "Your hard disk with Letter: " + drive + " is almost full, please replace the disk or cleanup!";
  26.             SmtpClient client = new SmtpClient("MailServer.company.com")
  27.             {
  28.                 EnableSsl = false
  29.             };
  30.  
  31.             client.Send(message);
  32.         }
  33.  
  34.         static void Main(string[] args)
  35.         {
  36.             string drive = @"C:\";
  37.             if (CheckTotalFreeSpaceRate(drive))
  38.             {
  39.                 SendEmail(drive);
  40.             }
  41.         }
  42.     }
  43. }

回复 "监视所在的计算机的磁盘使用率是否超过设定百分率"

这儿你可以回复上面这条便签

captcha