How do I get Hard Drive Information using VB .NET?

Posted on 2010-09-24
Last Modified: 2012-05-10

I've been searching the internet for two days and I'm finally going to breakdown and ask this question because I can't find the answers.

I am trying to write a simple VB .Net program (it needs to be simple because I'm not that experienced) that will detect a USB storage device and then run another program to put an image on it. This other program is a console app and it needs to have the drive plugged into physical drive 1.  So far, I have been able to detect that there is a drive plugged into physical drive 1 using CreateFile().  Now I have two problems:

1.  If I put a loop into my form load event to wait for the drive to be plugged in, nothing on the form shows up.  For example, I want to display a message telling the user to plug the drive in, but the form is blank.  I can't figure out which event to use or how I can use a module to do this.  Whenever the loop starts, the form is blank.  I'm sure this is simple, but I can't find it.

2.  This one is a bit more complicated.  The second thing I need to do after the drive is plugged in is to verify that it's the right model number and manufacturer and get the capacity so I'll know which image to load and so on.  Once I have the device handle, how do I get the rest of the information?

Any help would be much appreciated.  I'm basically just trying to make a GUI interface to this other program.  I thought this would be easy, but it's turning out to be more complicated.

Thanks and regards,
Imports System.Management

Public Class Form1
    Private Const INVALID_HANDLE = -1
    Private Const GENERIC_READ = &H80000000
    Private Const GENERIC_WRITE = &H40000000
    Private Const FILE_SHARE_READ = 1
    Private Const FILE_SHARE_WRITE = 2
    Private Const CREATE_ALWAYS = 2
    Private Const OPEN_EXISTING = 3
    Private Const INIT_SUCCESS = 0
    Private Const WRITE_ERROR = 0
    Private Const READ_ERROR = 0

    Private Declare Function CreateFile _
    Lib "kernel32" Alias "CreateFileA" _
    (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
     ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As Int32, _
     ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
     ByVal hTemplateFile As Int32) As Int32

    Private Declare Function WriteFile _
     Lib "kernel32" _
     (ByVal hFile As Int32, ByRef lpBuffer As Int32, ByVal nNumberOfBytesToWrite As Int32, _
      ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As Int32) As Int32

    Private Declare Function ReadFile _
     Lib "kernel32" _
     (ByVal hFile As Int32, ByRef lpBuffer As String, ByVal nNumberOfBytesToRead As Int32, _
     ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As Int32) As Int32

    Private hDevice As Int32
    Private hTarget As Int32

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

        'make sure nothing else is plugged into the USB ports, since we need the drive to be physical drive 1
        MsgBox("Make sure no USB Storage devices are plugged into this system!", MsgBoxStyle.Exclamation, "Warning")

        'get the intitial device handle for physical drive 1 - should be -1
        hDevice = CreateFile("\\.\PHYSICALDRIVE1", GENERIC_READ, _
            FILE_SHARE_READ, 0&, OPEN_EXISTING, 0&, 0&)

        'now loop until there is something plugged into physical drive 1
        While hDevice = -1
            hDevice = CreateFile("\\.\PHYSICALDRIVE1", GENERIC_READ, _
            FILE_SHARE_READ, 0&, OPEN_EXISTING, 0&, 0&)
            Me.TextBox1.Text = "Please plug in the USB drive."
        End While

    End Sub

    Private Sub btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OK.Click

        'Ideally, I want to detect the capacity to load the proper image, but for now, this is what I came up with.

        Dim command As String

        If rb1.Checked Then
            'run the program to load image 1
            command = "C:\imageload.exe image1"
        End If
        If rb2.Checked Then
            'run the program to load image 2
            command = command = "C:\imageload.exe image2"
        End If
        If rb3.Checked Then
            'run the program to load image 2
            command = command = "C:\imageload.exe image2"
        End If

        'I am using shell for now, but will move to the process.start so I can make it invisible to the user.

    End Sub

    Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click
    End Sub

End Class

Open in new window

Question by:EQUINN
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Author Comment

ID: 33755834
Sorry, I forgot to mention another thing.  The drive will typically be blank, so I can't go off the drive letter.

Accepted Solution

EQUINN earned 0 total points
ID: 33757568
OK, I have figured out how to get the information from the drive.  I'm using the WMI ManagementObjectSearcher ("select * from Win32_DiskDrives").  That gives me the model, index (Physical Drive, capacity, manufacturer, and a bunch of other stuff I don't need.

 I still can't figure out how to create a loop to get the user to plug in the device and have it show up on the form.  Please help.  This is frustrating and I would think it is simple.  What am I missing?

Private strManufacturer As String
    Private strModel As String
    Private strInterface As String
    Private intIndex As Integer
    Private ulSize As ULong

Sub GetDriveInfo()

        Dim searcher As New ManagementObjectSearcher("select * from Win32_DiskDrive")

        For Each drive In searcher.Get
            strModel = drive("Model").ToString
            strInterface = drive("InterfaceType").ToString
            strManufacturer = drive("Manufacturer").ToString
            intIndex = drive("Index")
            ulSize = drive("Size")

            'MsgBox(strManufacturer & ", " & strInterface & ", " & strModel & ", " & intIndex & ", " & ulSize)
            If intIndex = 1 And strInterface = "USB" Then Exit For

    End Sub

Open in new window


Author Comment

ID: 33757978
Hi Guys,

Well I thought it would be simple, and it was.  I'm kinda kicking myself now (did I mention my lack of experience? : )

OK I put "Me.Show" at the beginning of the form load event, and then "Me.Refresh" inside the loop.  That seems to have done the trick.  I can now see the form and it shows that the device isn't plugged in yet and then it shows when the correct device is plugged in.  Voila!

Thanks anyway.  Hopefully, I can go from here.

Best regards,

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Samsung SSD 840 EVO and 840 EVO mSATA have a well-known problem with a drop in read performance. I first learned about this in an interesting thread here at Experts Exchange:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question