[c#]代码库
using System.IO;
using System.Net;
using System.Net.Mail;
namespace MonitorDiskspace
{
class Program
{
private static bool CheckTotalFreeSpaceRate(string driveName)
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == driveName)
return (drive.TotalFreeSpace * 100 / drive.TotalSize) < 15;
}
return false;
}
private static void SendEmail(string drive)
{
MailMessage message = new MailMessage();
message.To.Add("AppSupport@company.com");
message.Subject = "Alert!!! Disk Almost full";
message.From = new MailAddress("AppSupport@company.com");
message.Body = "Your hard disk with Letter: " + drive + " is almost full, please replace the disk or cleanup!";
SmtpClient client = new SmtpClient("MailServer.company.com")
{
EnableSsl = false
};
client.Send(message);
}
static void Main(string[] args)
{
string drive = @"C:\";
if (CheckTotalFreeSpaceRate(drive))
{
SendEmail(drive);
}
}
}
}