C# 監控硬碟空間
private void Form1_Load(object sender, EventArgs e)
{
string strtmp = string.Empty;
DriveInfo[] ListDrivesInfo = DriveInfo.GetDrives();
try
{
foreach (DriveInfo vListDrivesInfo in ListDrivesInfo)
{
if (vListDrivesInfo.IsReady && vListDrivesInfo.DriveType == DriveType.Fixed)
{
// vListDrivesInfo.Name + vListDrivesInfo.TotalFreeSpace.ToString());
double free_kilobytes = (double)(Int64)vListDrivesInfo.TotalFreeSpace / 1024.0;
double free_megabytes = free_kilobytes / 1024.0;
double free_gigabytes = free_megabytes / 1024.0;
strtmp += vListDrivesInfo.Name + " : " + free_gigabytes.ToString(".00") + " GB</br></br>";
}
}
}
catch (Exception ex)
{
strtmp = ex.Message;
}
sendmail(strtmp);
this.Close();
}
/// <summary>
/// 寄mail
/// </summary>
private void sendmail(string body)
{
try
{ //mail主機
SmtpClient sc = new SmtpClient("192.168.0.1");//<-宣告的時候可以先給主機名稱~記住喔~這是發送端的主機名稱~
// 設定port
sc.Port = 25;
//設定帳號,密碼
sc.Credentials = new System.Net.NetworkCredential("帳號", "密碼");
//設定收件人
MailAddress receiverAddress = new MailAddress("帳號@com.tw", "收件者");//<-這物件只是用來設定郵件帳號而已~
//設定寄件人
MailAddress senderAddress = new MailAddress("帳號@com.tw", "系統通知 監控 HD 空間"); //"寄件者-系統通知"
MailMessage mail = new MailMessage(senderAddress, receiverAddress);//<-這物件是郵件訊息的部分~需設定寄件人跟收件人~可直接打郵件帳號也可以使用MailAddress物件~
//mail主旨
mail.Subject = "硬碟空間";
//內文,可以用html的寫法,</br> 換行
mail.Body = body; //內文
mail.IsBodyHtml = true;//<-如果要這封郵件吃html的話~這屬性就把他設為true~~
////加入附件
// //Attachment attachment = new Attachment(@"C:\ERR_IMG\" + strjpgname + ".jpg");////<-這是附件部分~先用附件的物件把路徑指定進去~
// //mail.Attachments.Add(attachment);//<-郵件訊息中加入附件
sc.Send(mail);//<-這樣就送出去拉
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
沒有留言:
張貼留言