?
Solved

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

Posted on 2010-09-24
3
Medium Priority
?
2,037 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:EQUINN
[X]
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
3 Comments
 
LVL 1

Author Comment

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

Accepted Solution

by:
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?

Thanks,
EQUINN
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
        Next


    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:EQUINN
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,
EQUINN
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

770 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