Imports System.Management
Imports System.Environment
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(228, 15)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(113, 34)
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "空き容量の検索"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Button1)
        Me.GroupBox1.Location = New System.Drawing.Point(26, 172)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(381, 441)
        Me.GroupBox1.TabIndex = 5
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "検索結果"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.ListBox1)
        Me.GroupBox2.Location = New System.Drawing.Point(23, 10)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(199, 146)
        Me.GroupBox2.TabIndex = 6
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "ドライブCの情報"
        '
        'ListBox1
        '
        Me.ListBox1.ItemHeight = 12
        Me.ListBox1.Location = New System.Drawing.Point(12, 21)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(173, 100)
        Me.ListBox1.TabIndex = 1
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.Label1)
        Me.GroupBox3.Controls.Add(Me.ComboBox1)
        Me.GroupBox3.Location = New System.Drawing.Point(228, 17)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(176, 140)
        Me.GroupBox3.TabIndex = 7
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "ドライブの空き容量"
        '
        'Label1
        '
        Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Label1.Location = New System.Drawing.Point(14, 66)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(139, 31)
        Me.Label1.TabIndex = 4
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'ComboBox1
        '
        Me.ComboBox1.Location = New System.Drawing.Point(15, 28)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(143, 20)
        Me.ComboBox1.TabIndex = 3
        Me.ComboBox1.Text = "ドライブを選択"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(466, 634)
        Me.Controls.Add(Me.GroupBox3)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "Form1"
        Me.Text = "ドライブ情報"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim MyDrive() As String = GetLogicalDrives()
        Dim cnt As Byte = UBound(MyDrive)
        Dim i As Byte

        For i = 0 To cnt
            Me.ComboBox1.Items.Add(MyDrive(i))
        Next

        Dim mo As New ManagementObject("win32_logicaldisk.deviceid=""c:""")
        Dim pp As PropertyDataCollection
        Dim diskProperty As PropertyData

        pp = mo.Properties
        For Each diskProperty In pp
            Me.ListBox1.Items.Add(diskProperty.Name)
        Next diskProperty
    End Sub

    'GroupBoxに配置する場合
    Dim x As Integer = 20, y As Integer = 40

    '配列にデータを格納する
    Dim drvspace() As Double
    Dim drvsize() As Double
    Dim percent() As Double
    Dim drvnum As Byte
    Dim flag As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyDrive() As String = GetLogicalDrives()
        Dim cnt As Byte = UBound(MyDrive)
        Dim pp As PropertyDataCollection
        Dim diskProperty As PropertyData
        Dim i As Byte = 0
        Dim lb As System.Windows.Forms.Label

        Dim dname As String
        Dim arg As String

        For i = 0 To cnt
            ReDim Preserve drvspace(i)
            ReDim Preserve drvsize(i)
            ReDim Preserve percent(i)

            dname = Microsoft.VisualBasic.Left(MyDrive(i), 2)

            arg = "win32_logicaldisk.deviceid=" & """" & dname & """"

            Dim mo As New ManagementObject(arg)
            pp = mo.Properties

            drvspace(i) = Convert.ToDecimal(pp.Item("FreeSpace").Value)
            drvsize(i) = Convert.ToDecimal(pp.Item("Size").Value)
            If drvspace(i) <> 0 And drvsize(i) <> 0 Then
                percent(i) = drvspace(i) / drvsize(i) * 100
            Else
                percent(i) = 0
            End If

            lb = New System.Windows.Forms.Label
            lb.Width = 200
            lb.Text = MyDrive(i) & " : " & _
                      Format(drvspace(i) / 1000000000, "##0.00") & "GB" _
                      & " / " & Format(drvsize(i) / 1000000000, "##0.00") & "GB" _
                      & " : " & Format(percent(i), "##0.00") & "%"

            y += 50
            lb.Location = New Point(x, y)
            Me.GroupBox1.Controls.Add(lb)
        Next

        flag = True
        drvnum = i
        Me.GroupBox1.Refresh()

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim dname As String
        Dim arg As String
        Dim pp As PropertyDataCollection
        Dim diskProperty As PropertyData

        dname = Microsoft.VisualBasic.Left(ComboBox1.SelectedItem, 2)

        arg = "win32_logicaldisk.deviceid=" & """" & dname & """"

        Dim mo As New ManagementObject(arg)
        pp = mo.Properties
        Me.Label1.Text = Format((Convert.ToDecimal(pp.Item("FreeSpace").Value) / 1000000000), "###.00") & "GB"

    End Sub

    Private Sub GroupBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupBox1.Paint
        Dim x As Short = 250, y As Short = 30
        Dim i As Byte
        Dim sizebrush As New SolidBrush(Color.Azure)
        Dim freebrush As Brush

        If flag = True Then
            For i = 0 To drvnum - 1

                y += 50
                Dim sizerect As New Rectangle(x, y, 100, 30)
                e.Graphics.FillRectangle(sizebrush, sizerect)

                '空き容量0の場合
                If percent(i) <= 0 Then
                    freebrush = New SolidBrush(Color.LightBlue)
                Else
                    freebrush = New SolidBrush(Color.BlueViolet)
                End If

                Dim freerect As New Rectangle(x, y, 100 - percent(i), 30)
                e.Graphics.FillRectangle(freebrush, freerect)
            Next
        End If
    End Sub
End Class
