Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Disk Alert with Email-notification

4.00/5 (2 votes)
14 Apr 2013CPOL 14.5K   728  
Disk Space Alert for Server Backup with email-notification

Introduction

If you want to know if one of your disks is almost full in one of your server backups, this code does it with a chart like Windows does.

Using the Code

Add a chart to your Windows Form and create variables for size, freespace, and usedspace.

C#
public void LoadChars() 
{
    //drive1 is the driver letter of the disk
    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + 
      drive1.Substring(0, 2) + "\"");
    disk.Get();
    //Get the size
    size = double.Parse(disk["Size"].ToString());
    //convert the size from bytes to GB 
    size = Math.Round(size / 1000000000, 2);
    //Get the freeSpace
    freeSpace = double.Parse(disk["FreeSpace"].ToString());
    //convert the size from bytes to GB 
    freeSpace = Math.Round(freeSpace / 1000000000, 2);
    //Get the usedspace
    usedspace = (size - freeSpace);

    this.chart1.Palette = ChartColorPalette.SeaGreen;

    // Set title.
    this.chart1.Titles.Clear();
    this.chart1.Titles.Add("Disk Information");
    Series series = new Series();
    // Add series.
    chart1.Series.Clear();
    chart1.Legends[0].Alignment = StringAlignment.Center;
    chart1.Palette = ChartColorPalette.Bright;
    chart1.BackColor = Color.AliceBlue;
    //Chart setting
    chart1.ChartAreas[0].BackColor = Color.Transparent;
    Series series1 = new Series
    {
        Name = "series1",
        IsVisibleInLegend = true,
        Color = System.Drawing.Color.Green,
        ChartType = SeriesChartType.Pie
    };
    series1.Font = new Font(FontFamily.GenericSerif, 11, FontStyle.Bold);

    chart1.BorderlineColor = Color.Aquamarine;
    chart1.Series.Add(series1);
    series1.Points.Add(freeSpace);
    series1.Points.Add(usedspace);
    var p1 = series1.Points[0];
    p1.AxisLabel = freeSpace.ToString();
    p1.LegendText = "Free Space";
    var p2 = series1.Points[1];
    p2.AxisLabel = usedspace.ToString();
    p2.LegendText = "Used Space";
    chart1.Invalidate();
}


public void SendEmail() {
    if ((freeSpace*100/size)<15)
    {
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        message.To.Add(destinationemail@domain.com);
        message.Subject = "Alert!!! Disk Almost full";
        message.From = new System.Net.Mail.MailAddress("your-email@gmail.com");
        message.Body = "Your hard disk with Letter: " + drive1 + 
        " is almost full, please replace the disk or cleanup";
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential
            ("your-email@gmail.com", "password"),
            EnableSsl = true
        };
    
        client.Send(message);
    }
    ...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)