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
.
public void LoadChars()
{
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" +
drive1.Substring(0, 2) + "\"");
disk.Get();
size = double.Parse(disk["Size"].ToString());
size = Math.Round(size / 1000000000, 2);
freeSpace = double.Parse(disk["FreeSpace"].ToString());
freeSpace = Math.Round(freeSpace / 1000000000, 2);
usedspace = (size - freeSpace);
this.chart1.Palette = ChartColorPalette.SeaGreen;
this.chart1.Titles.Clear();
this.chart1.Titles.Add("Disk Information");
Series series = new Series();
chart1.Series.Clear();
chart1.Legends[0].Alignment = StringAlignment.Center;
chart1.Palette = ChartColorPalette.Bright;
chart1.BackColor = Color.AliceBlue;
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);
}
...