Link to home
Start Free TrialLog in
Avatar of Elena Quinn
Elena Quinn

asked on

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

Hi,

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,
EQUINN
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.
        Shell(command)

    End Sub


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


End Class

Open in new window

Avatar of Elena Quinn
Elena Quinn

ASKER

Sorry, I forgot to mention another thing.  The drive will typically be blank, so I can't go off the drive letter.
ASKER CERTIFIED SOLUTION
Avatar of Elena Quinn
Elena Quinn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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,
EQUINN