?
Solved

VB.Net find Hard Disk Number (Like in Disk Manager)

Posted on 2009-07-08
5
Medium Priority
?
801 Views
Last Modified: 2012-06-27
I need a way to determine the Disk Number of a drive based on it's letter.

For example, if you open 'Computer Management', then got to 'Disk Management', then look at the listing of drives (on the bottom), they are 'Disk 0', 'Disk 1', and so on. (or right-click the 'Disk 0', go to properties, then click the 'Volumes' tab - look at 'Disk:' - that's the number I need - same as the other area)

I need to be able to determine the disk number by drive letter, for hard disks and memory cards.

(This number will be passed to Ghost to backup the disk)
0
Comment
Question by:Crazy_Penguins
[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
5 Comments
 
LVL 15

Accepted Solution

by:
JackOfPH earned 2000 total points
ID: 24809696
Try this one...

Private Declare Function GetVolumeInformation Lib _
"kernel32.dll" Alias "GetVolumeInformationA" (ByVal _
lpRootPathName As String, ByVal lpVolumeNameBuffer As _
String, ByVal nVolumeNameSize As Integer, _
lpVolumeSerialNumber As Long, lpMaximumComponentLength _
As Long, lpFileSystemFlags As Long, ByVal _
lpFileSystemNameBuffer As String, ByVal _
nFileSystemNameSize As Long) As Long

Function GetSerialNumber(strDrive As String) As Long
Dim SerialNum As Long
Dim Res As Long
Dim Temp1 As String
Dim Temp2 As String
Temp1 = String$(255, Chr$(0))
Temp2 = String$(255, Chr$(0))

Res = GetVolumeInformation(strDrive, Temp1, _
Len(Temp1), SerialNum, 0, 0, Temp2, Len(Temp2))

GetSerialNumber = SerialNum

End Function

Private Sub cmdSerial_Click()
Call MsgBox(GetSerialNumber("C:\"))
End Sub
0
 

Author Comment

by:Crazy_Penguins
ID: 24809878
Not quite what I am after, perhaps if you look at this picture - I want where it says "Disk 2"
Disk.PNG
0
 

Author Comment

by:Crazy_Penguins
ID: 24810038
After much trial and error, error, error I found out that Ghost does not want that drive ID anyhow.
Got my solution by just running GDISK32.exe (with ghost) and will get my list that way - below is a sample output.

Thanks for the input,

Andrew
Disk  Partitions  Cylinders  Heads  Sectors  Mbytes  Model
  1        1        60801     255      63  476940.0  Hitachi HDP725050GLA GM4O
  2        1        24321     255      63  190782.2  MAXTOR S TM3200820AS 3.AA
  3        2        14593     255      63  114473.5  Maxtor OneTouch 0125

Open in new window

0
 

Author Closing Comment

by:Crazy_Penguins
ID: 31601378
Thanks for looking
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 24819136
This question is closed but for the question PAQ. Use IOCTL_STORAGE_GET_DEVICE_NUMBER
Option Explicit
 
Private Const IOCTL_STORAGE_GET_DEVICE_NUMBER As Long = &H2D1080
Private Const FILE_DEVICE_CD_ROM As Long = &H2
Private Const FILE_DEVICE_DISK As Long = &H7
Private Const INVALID_HANDLE_VALUE As Long = (-1)
Private Const FILE_SHARE_READ As Long = &H1
Private Const FILE_SHARE_WRITE As Long = &H2
Private Const OPEN_EXISTING As Long = 3
 
Private Type STORAGE_DEVICE_NUMBER
DeviceType As Long 'Specifies one of the system-defined FILE_DEVICE_XXX constants
DeviceNumber As Long 'Indicates the number of this device.
PartitionNumber As Long
End Type
 
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByVal lpInBuffer As Long, ByVal nInBufferSize As Long, ByVal lpOutBuffer As Long, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
 
Sub GetStorageDeviceNumber(ByVal Device As String)
  
  ' Device: The device letter or volume guid. example. c: or d:
  
  Dim sdn As STORAGE_DEVICE_NUMBER
  Dim hDevice As Long
  Dim cbRead As Long
  
  ' The dwDesiredAccess parameter can be zero, allowing the application to query device attributes without accessing a device.
  ' It can also be used for reading statistics without requiring higher-level data read/write permission.
  hDevice = CreateFileW(StrPtr("\\.\" & Device), 0, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  
  If hDevice = INVALID_HANDLE_VALUE Then
    Exit Sub
  End If
 
  If DeviceIoControl(hDevice, IOCTL_STORAGE_GET_DEVICE_NUMBER, ByVal 0, 0, VarPtr(sdn), LenB(sdn), cbRead, 0) Then
    ' FILE_DEVICE_XXX
    ' http://msdn.microsoft.com/en-us/library/ms794701.aspx
    Select Case sdn.DeviceType
      Case FILE_DEVICE_CD_ROM
        MsgBox "CDROM " & sdn.DeviceNumber
      Case FILE_DEVICE_DISK
        MsgBox "DISK " & sdn.DeviceNumber & " (Partition)= " & sdn.PartitionNumber
    End Select
  End If
  ' cleanup
  CloseHandle hDevice
 
End Sub

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

765 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