WebSite  
 
Home Page > IT School

Determining a Drive's Characteristics

03-02-2008
Article by:
Pradeep Mamgain

Do you like this Article?
Excellent
Good
Poor
Dad

Determining a Drive's Characteristics

API, DRIVE, GETDRIVETYPE

At times you want to have know the physical profile and space characterstics of the drives on your system, like before adding to a database or writing a large amount of Information to the Drive. This article will lead you through, creating such an application using API Calls.

Technique

You need a program that first determines the valid local and networked drives. Then call an API function to get the drive characteristics. After you have all the information you can compute the total size and amount of free space on a particular drive.

Steps

Start a New project and add a module to it. Add a combo box to the form and an indexed label control, index value up to 6. Add following code to general declaration area of module.

Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long
Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Const DRIVE_UNKNOWN = 0
Public Const DRIVE_NOTEXIST = 1
Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_FIXED = 3
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_CDROM = 5
Public Const DRIVE_RAMDISK = 6

Add following code to form's Load Event. First, we have to get a Drive List delimited by a NULL character. Drive letters are three characters long and include the : and / punctuation. So total length that a drive will occupy is 4 chars, like C + : + | + Null Char. The drive list will have a final null at the end of the string so total buffer length would be 26 * 4 +1

Private Sub Form_Load()
Dim strDrivesNames As String
Dim lngBuffer As Long
Dim lngDriveReturn As Long
Dim tempString As String

'Get the drive name
'Compute the string length
lngBuffer = 26 * 4 + 1

'allocate the buffer space
strDrivesNames = Space(lngBuffer)
lngDriveReturn = GetLogicalDriveStrings(lngBuffer, strDrivesNames)

Dim I As Integer
I = 1
Do
'Peel out first drive string
tempString = Mid(strDrivesNames, I, 3)

If Mid(tempString, 1, 1) = vbNullChar Then Exit Do

'add the drive to drop down combo
cmbDrive.AddItem tempString

'Increase I by 4 to goto next drive
I = I + 4
Loop
'select the first drive
cmbDrive.ListIndex = 1
End Sub

Place the following cod in the Click Event of the Combo control. This function will get a drive letter from the combo control and will pass it to API function to get a drive letter. Next, the GetDiskFreeSpace API function is called to get the drive's physical characterstics.

Private Sub cmbDrive_Click()
Dim lngSectorPerCluster As Long
Dim lngBytesPerSector As Long
Dim lngFreeClusters As Long
Dim lngTotalClusters As Long
Dim lngReturn As Long
Dim strDrive As String
Dim lngMB As Long
Dim dblSize As Double

'Get the currently selected Drive
strDrive = cmbDrive.List(cmbDrive.ListIndex)

'Get the Drive Type
lngReturn = GetDriveType(strDrive)

'Evaluate the returned drive type
Select Case lngReturn
Case DRIVE_UNKNOWN
lblInfo(0).Caption = "Unknown"
Case DRIVE_NOTEXIST
lblInfo(0).Caption = "Not Found"
Case DRIVE_REMOVABLE
lblInfo(0).Caption = "Removable"
Case DRIVE_FIXED
lblInfo(0).Caption = "Fixed"
Case DRIVE_REMOTE
lblInfo(0).Caption = "Remote"
Case DRIVE_RAMDISK
lblInfo(0).Caption = "Ram Disk"
Case DRIVE_CDROM
lblInfo(0).Caption = "CD-ROM"
End Select

'Get the Drive Stats

lngReturn = GetDiskFreeSpace(strDrive, lngSectorPerCluster, lngBytesPerSector, lngFreeClusters, lngTotalClusters)

lblInfo(1).Caption = lngSectorPerCluster
lblInfo(2).Caption = lngBytesPerSector
lblInfo(3).Caption = lngFreeClusters
lblInfo(4).Caption = lngTotalClusters

'Compute a megabyte
lngMB = 1024 ^ 2


'Compute total Free MB
dblSize = CDbl(lngSectorPerCluster) * CDbl(lngBytesPerSector) * CDbl(lngFreeClusters)

lblInfo(5).Caption = Format(dblSize / lngMB, "###,###0")

'Compute Full Capacity
dblSize = CDbl(lngSectorPerCluster) * CDbl(lngBytesPerSector) * CDbl(lngTotalClusters)

lblInfo(6).Caption = Format(dblSize / lngMB, "###,###0")

End Sub

To make a program that monitors drive's stats proactive, add a timer control that fires every minute or so.




 User Comments                                                                                



 Other Articles

Your name:
Your email:
Subject:
Comment Text:



 Photo Gallery
 
Latest IT News
Google scores 61% of the search market Google scores 61% of...

The guys at the ‘plex must be rubbing their hands with glee over the figures from ComScore’s ‘first comprehensive study...

Would your Firefox need mouse gestures? Would your Firefox need...

I remember when I first tried Opera, that was supposed to be “the fastest browser on Earth”, I accidentally...

The Top 12 Signs That Your Computer Has Spyware The Top 12 Signs That...

How to tell if your computer has spyware on it. Sign Number...

How to create Screensaver from Flash movie? How to create...

Have you ever thought your Flash movie would make a great...

7 very Important Ways To Prevent Spam 7 very Important Ways...

Spam is the bane of everyone’s existence. Unsolicited mail...

Keeping Your Home PC Virus-Free Keeping Your Home PC...

There are thousands of them and more appear every day. They...

 
 
Africaninet.com  Copyright © 2006 Africaninet.com  |Terms of Use for Africaninet template and Privacy Policy and Safety Information/ All rights reserved.
Powered by: PHPCow.com