Solved

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

Posted on 2010-09-24
3
1,888 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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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: http://www.experts-exchange.com/Hardware/Storage/Hard_Drives/Q_2852…
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

948 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now