Advertisement
Guest User

Space Checker

a guest
Nov 25th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Net.Mail;
  9.  
  10. namespace Storage_Space_Checker
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.  
  17.             string drive = @"C:\";
  18.             float alertIfLower = 1.0f;
  19.  
  20.             bool hasAlerted = false;
  21.  
  22.             if (args.Length == 2)
  23.             {
  24.                 drive = args[0];
  25.                 alertIfLower = float.Parse(args[1]);
  26.             }
  27.  
  28.             while (true)
  29.             {
  30.                 foreach (DriveInfo d in DriveInfo.GetDrives())
  31.                 {
  32.                     if (d.Name == drive)
  33.                     {
  34.                         float freeSpace = d.TotalFreeSpace / 1024f / 1024f / 1024f;
  35.                         Console.WriteLine(freeSpace.ToString("####.00"));
  36.  
  37.                         if (freeSpace < alertIfLower)
  38.                         {
  39.                             if (!hasAlerted)
  40.                             {
  41.                                 MailMessage mail = new MailMessage("[email protected]", "[email protected]");
  42.                                 SmtpClient client = new SmtpClient();
  43.                                 client.Port = 25;
  44.                                 client.DeliveryMethod = SmtpDeliveryMethod.Network;
  45.                                 client.UseDefaultCredentials = false;
  46.                                 client.Host = "192.168.1.5";
  47.                                 mail.Subject = "Disk Space Full!";
  48.                                 mail.Body = "Disk space on drive " + drive + " in computer " + Environment.MachineName + " is full.";
  49.                                 client.Send(mail);
  50.                                 hasAlerted = true;
  51.                             }
  52.                         }
  53.                         else
  54.                         {
  55.                             hasAlerted = false;
  56.                         }
  57.                     }
  58.                 }
  59.  
  60.                 Thread.Sleep(5000);
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement