Solved

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

Posted on 2010-09-24
3
1,868 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

12 Experts available now in Live!

Get 1:1 Help Now