Cari di Blog Ini

Senin, 31 Maret 2014

VB.NET : Mendapatkan Serial Number, Volume dan Size pada Hard Disk/Flash Disk/CD ROM/DVD ROM

Untuk mengetahui Serial Number, Volume dan Size pada Hard Disk/Flash Disk/CD ROM/DVD ROM menggunakan VB.NET caranya sbb :
Imports System.Reflection
Imports System.IO

Public Class Form1
    Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Integer, ByRef lpMaximumComponentLength As Integer, ByRef lpFileSystemFlags As Integer, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Integer) As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        LstDrive.Items.Clear()
        LstVolLabel.Items.Clear()
        LstSize.Items.Clear()
        LstDriveType.Items.Clear()
        LstPartitionType.Items.Clear()
        LstVolumeID.Items.Clear()

        Dim allDrives() As IO.DriveInfo = IO.DriveInfo.GetDrives()

        Dim drive As IO.DriveInfo
        LblDriveNumber.Text = allDrives.Count & " Drive(s) detected"
        For Each drive In allDrives

            LstDrive.Items.Add(drive.RootDirectory) '.ToString()

            Try
                LstVolLabel.Items.Add(drive.VolumeLabel) '.ToString()
                LstSize.Items.Add(Format(drive.AvailableFreeSpace / (1024 * 1024), "###,##0 GB") _
                             & " of " & Format(drive.TotalSize / (1024 * 1024), "###,##0 GB") _
                             & " (" & Format(drive.AvailableFreeSpace / drive.TotalSize, "###,##0% used)"))

                If drive.DriveType = IO.DriveType.Removable Then
                    LstDriveType.Items.Add("USB")
                ElseIf drive.DriveType = IO.DriveType.Network Then
                    LstDriveType.Items.Add("Network")
                ElseIf drive.DriveType = IO.DriveType.Fixed Then
                    LstDriveType.Items.Add("HD")
                ElseIf drive.DriveType = IO.DriveType.CDRom Then
                    LstDriveType.Items.Add("CD-ROM")
                ElseIf drive.DriveType = IO.DriveType.Unknown Then
                    LstDriveType.Items.Add("???")
                End If
                LstPartitionType.Items.Add(drive.DriveFormat)
                check_ID(drive.RootDirectory)

            Catch ex As Exception
                LstVolLabel.Items.Add("")
                LstSize.Items.Add("")
                LstDriveType.Items.Add("")
                LstPartitionType.Items.Add("")
                LstVolumeID.Items.Add("")
            End Try

        Next

    End Sub
    Private Sub check_ID(ByVal cur_drive)

        Dim root As String
        Dim volume_name As String
        Dim serial_number As Integer
        Dim max_component_length As Integer
        Dim file_system_flags As Integer
        Dim file_system_name As String
        Dim pos As Integer
        Dim Vol_ID As String
        root = cur_drive.ToString
        'ListBox6.Items.Add(root)
        volume_name = Space(1024)
        file_system_name = Space(1024)

        If GetVolumeInformation(root, volume_name, Len(volume_name), serial_number, max_component_length, file_system_flags, file_system_name, Len(file_system_name)) = 0 Then
            MessageBox.Show("Tidak ada drive atau formatnya salah ( harus pakai tanda :\ )", "Error Getting Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        pos = volume_name.IndexOf(Chr(0))
        volume_name = volume_name.Substring(0, pos)
        Vol_ID = Hex(serial_number.ToString)
        Vol_ID = Microsoft.VisualBasic.Left(Vol_ID, 4) & "-" & Microsoft.VisualBasic.Right(Vol_ID, 4)
        LstVolumeID.Items.Add(Vol_ID)

        pos = file_system_name.IndexOf(Chr(0))
        file_system_name = file_system_name.Substring(0, pos)
    End Sub
End Class

Selamat mencoba :)