最近因為有發生過磁碟空間不足造成系統就掛了

印象中以前也發生過,後來主管要求定期發Mail通知目前硬碟空間

如果有不足要馬上反應......

後來只好用了一支小程式去處理這件事啦

順便也把程式記錄下來

不過如果是Mail Server掛點了......那就也沒辦法了吧我想~

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim allDrives() As System.IO.DriveInfo = System.IO.DriveInfo.GetDrives()
        Dim d As System.IO.DriveInfo
        Dim strMsg As String = ""
        For Each d In allDrives
            strMsg = strMsg & String.Format("磁碟名稱: {0} 
類別: {1}
", d.Name, d.DriveType) '磁碟是否就緒 If d.IsReady = True Then strMsg = strMsg & String.Format("
磁碟區標籤: {0}", d.VolumeLabel) strMsg = strMsg & String.Format("
檔案系統(NTFS OR FAT): {0}", d.DriveFormat) strMsg = strMsg & String.Format("
目前可用空間:{0, 15} GB", (((d.AvailableFreeSpace / 1024) / 1024) / 1024).ToString("#.#0")) strMsg = strMsg & String.Format("
所有可用空間:{0, 15} GB", (((d.TotalFreeSpace / 1024) / 1024) / 1024).ToString("#.#0")) strMsg = strMsg & String.Format("
磁碟空間容量:{0, 15} GB", (((d.TotalSize / 1024) / 1024) / 1024).ToString("#.#0")) End If Next Call SendMail(strMsg) Me.Close() Me.Dispose() End Sub Public Shared Sub SendMail(ByVal strBody As String) Try Dim insMail As New Net.Mail.MailMessage() Dim SmtpMail As New Net.Mail.SmtpClient() With insMail .From = New Net.Mail.MailAddress("xxx@xxxx.xxx") .To.Add("xxx@xxxx.xxx") .Subject = "磁碟空間回報" .Body = strBody .CC.Add("xxx@xxxx.xxx") .IsBodyHtml = True 'If Not strAttachments.Equals(String.Empty) Then ' Dim strFile As String ' Dim strAttach() As String = strAttachments.Split(";") ' For Each strFile In strAttach ' .Attachments.Add(New Net.Mail.Attachment(strFile.Trim())) ' Next 'End If End With SmtpMail.Host = "MS1" SmtpMail.Send(insMail) Catch e As Exception Throw New Exception(e.Message) End Try End Sub

鴨爸 發表在 痞客邦 留言(0) 人氣()