2017年5月7日 星期日

讀取磁碟機資訊DriveInfo

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

沒有留言:

張貼留言