954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Size of Logical drives

Hey Guys, I am trying to create this application which can display the free space and the total space of logical drives,,I am very close but there is some little mistake which I am not being able to find out..Please help,,here is the code

Public Class Form2

    Private strDrive As String = ""
    Private lngFreeSpace As Double = 0
    Private lngTotalSpace As Double = 0
    
   
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

        FindDrives()
        ComboBox2.SelectedIndex = 0
        strDrive = ComboBox1.Text.Substring(0, 2)
        'DriveInfoSystem info=DriveInfo.GetInfo(strDrive);
        Dim info As DriveInfoSystem = DriveInfo.GetInfo("C:\Program Files")
        lngFreeSpace = Convert.ToDouble(info.Available)
        lngTotalSpace = Convert.ToDouble(info.Total)
        Label5.Text = lngFreeSpace.ToString()
        Label6.Text = lngTotalSpace.ToString()
    End Sub
    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        Conversion()
    End Sub

    Private Sub ComboBox2_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectionChangeCommitted
        Conversion()
    End Sub
    Private Sub Conversion()
        strDrive = ComboBox1.Text.Substring(0, 2)
        Dim info As DriveInfoSystem = DriveInfo.GetInfo(strDrive)
        lngFreeSpace = Convert.ToDouble(info.Available)
        lngTotalSpace = Convert.ToDouble(info.Total)

        Dim intIndex As Integer = ComboBox2.SelectedIndex

        Select Case intIndex

            Case 1
                lngFreeSpace = lngFreeSpace / 1024
                lngTotalSpace = lngTotalSpace / 1024
                Exit Select
            Case 2
                lngFreeSpace = (lngFreeSpace / 1024) / 1024
                lngTotalSpace = (lngTotalSpace / 1024) / 1024
                Exit Select
            Case 3
                lngFreeSpace = ((lngFreeSpace / 1024) / 1024) / 1024
                lngTotalSpace = ((lngTotalSpace / 1024) / 1024) / 1024
                Exit Select
            Case 4
                lngFreeSpace = (((lngFreeSpace / 1024) / 1024) / 1024) / 1024
                lngTotalSpace = (((lngTotalSpace / 1024) / 1024) / 1024) / 1024
                Exit Select
        End Select

        Label5.Text = lngFreeSpace.ToString()
        Label6.Text = lngTotalSpace.ToString()
    End Sub
    Private Sub FindDrives()
        Dim tempString As String() = Directory.GetLogicalDrives()
        For Each tempDrive As String In tempString
            ComboBox1.Items.Add(tempDrive)
        Next
        ComboBox1.SelectedIndex = 0
    End Sub
    Public Structure DriveInfoSystem
        Public ReadOnly Drive As String
        Public ReadOnly Result As Long
        Public ReadOnly Available As Long
        Public ReadOnly Total As Long
        Public ReadOnly Free As Long

        Public Sub New(ByVal drive As String, ByVal result As Long, ByVal available As Long, ByVal total As Long, ByVal free As Long)
            Me.Drive = drive
            Me.Result = result
            Me.Available = available
            Me.Total = total
            Me.Free = free
        End Sub
    End Structure
    Public NotInheritable Class DriveInfo
        Private Drive As String
        Private Result As Long
        Private Available As Long
        Private Total As Long
        Private Free As Long
        Public Sub New(ByVal drive As String, ByVal result As Long, ByVal available As Long, ByVal total As Long, ByVal free As Long)
            Me.Drive = drive
            Me.Result = result
            Me.Available = available
            Me.Total = total
            Me.Free = free
        End Sub
       
        <DllImport("kernel32.dll", EntryPoint:="GetDiskFreeSpaceExA")> _
        Private Shared Function GetDiskFreeSpaceEx(ByVal lpDirectoryName As String, ByRef lpFreeBytesAvailableToCaller As Long, ByRef lpTotalNumberOfBytes As Long, ByRef lpTotalNumberOfFreeBytes As Long) As Long
        End Function

        Public Shared Function GetInfo(ByVal drive As String, ByRef available As Long, ByRef total As Long, ByRef free As Long) As Long
            Return GetDiskFreeSpaceEx(drive, available, total, free)
        End Function


        Public Shared Function GetInfo(ByVal drive As String) As DriveInfoSystem
            Dim result As Long, available As Long, total As Long, free As Long
            result = GetDiskFreeSpaceEx(drive, available, total, free)
            Return New DriveInfoSystem(drive, result, available, total, free)
        End Function
    End Class

End Class
bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

It would really help if you told us what you were missing.

By the way, it is not necessary to have "End Select" in every case clause. Only one of the case clauses will ever be executed.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

I am not able to retrieve the drives..i guess something wrong with the driveinfo class..I will be thankful for any concerned help..

bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

I'm using VB 2010 and the following console app on my computer:

Module Module1

    Sub Main()

        For Each drive As String In System.Environment.GetLogicalDrives()

            Try

                Dim info As System.IO.DriveInfo = My.Computer.FileSystem.GetDriveInfo(drive)

                Console.WriteLine()
                Console.WriteLine("Name               " & info.Name.ToString)
                Console.WriteLine("VolumeLabel        " & info.VolumeLabel.ToString)
                Console.WriteLine("DriveType          " & info.DriveType.ToString)
                Console.WriteLine("DriveFormat        " & info.DriveFormat.ToString)
                Console.WriteLine("AvailableFreeSpace " & info.AvailableFreeSpace.ToString)
                Console.WriteLine("RootDirectory      " & info.RootDirectory.ToString)
                Console.WriteLine("TotalFreeSpace     " & info.TotalFreeSpace.ToString)
                Console.WriteLine("TotalSize          " & info.TotalSize.ToString)

            Catch e As System.IO.IOException

                Console.WriteLine()
                Console.WriteLine("Drive " & drive & " not available")

            End Try

        Next

    End Sub

End Module

Generates the following:

Name               C:\
VolumeLabel        C-OS
DriveType          Fixed
DriveFormat        NTFS
AvailableFreeSpace 7191638016
RootDirectory      C:\
TotalFreeSpace     7191638016
TotalSize          42443206656

Name               D:\
VolumeLabel        D-DATA
DriveType          Fixed
DriveFormat        NTFS
AvailableFreeSpace 42670780416
RootDirectory      D:\
TotalFreeSpace     42670780416
TotalSize          205294755840

Name               E:\
VolumeLabel        E-DATA
DriveType          Fixed
DriveFormat        NTFS
AvailableFreeSpace 62134550528
RootDirectory      E:\
TotalFreeSpace     62134550528
TotalSize          250056704000

Name               F:\
VolumeLabel        Roxio Creator DE
DriveType          CDRom
DriveFormat        CDFS
AvailableFreeSpace 0
RootDirectory      F:\
TotalFreeSpace     0
TotalSize          146671616

Name               W:\

Drive W:\ not available

Note that I am using slightly different system calls than you are. Try my code on your machine and tell me what you get.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

thanks for the replies...but now i have recoded it and everything's good about it,still i am not getting the desired output ,,most importantly not getting the drives listed.Here's the code:

Public Class Form2

    Private strDrive As String = ""
    Private lngFreeSpace As Double = 0
    Private lngTotalSpace As Double = 0
    
   
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

        FindDrives()
        ComboBox2.SelectedIndex = 0
        strDrive = ComboBox1.Text.Substring(0, 2)
        'DriveInfoSystem info=DriveInfo.GetInfo(strDrive);
        Dim info As System.IO.DriveInfo = My.Computer.FileSystem.GetDriveInfo(strDrive)

        lngFreeSpace = Convert.ToDouble(info.AvailableFreeSpace)
        lngTotalSpace = Convert.ToDouble(info.TotalFreeSpace)
        Label5.Text = lngFreeSpace.ToString()
        Label6.Text = lngTotalSpace.ToString()
    End Sub
    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
        Conversion()
    End Sub

    Private Sub ComboBox2_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectionChangeCommitted
        Conversion()
    End Sub
    Private Sub Conversion()
        strDrive = ComboBox1.Text.Substring(0, 2)
        Dim info As System.IO.DriveInfo = My.Computer.FileSystem.GetDriveInfo(strDrive)
        lngFreeSpace = Convert.ToDouble(info.AvailableFreeSpace)
        lngTotalSpace = Convert.ToDouble(info.TotalFreeSpace)

        Dim intIndex As Integer = ComboBox2.SelectedIndex

        Select Case intIndex

            Case 1
                lngFreeSpace = lngFreeSpace / 1024
                lngTotalSpace = lngTotalSpace / 1024
                Exit Select
            Case 2
                lngFreeSpace = (lngFreeSpace / 1024) / 1024
                lngTotalSpace = (lngTotalSpace / 1024) / 1024
                Exit Select
            Case 3
                lngFreeSpace = ((lngFreeSpace / 1024) / 1024) / 1024
                lngTotalSpace = ((lngTotalSpace / 1024) / 1024) / 1024
                Exit Select
            Case 4
                lngFreeSpace = (((lngFreeSpace / 1024) / 1024) / 1024) / 1024
                lngTotalSpace = (((lngTotalSpace / 1024) / 1024) / 1024) / 1024
                Exit Select
        End Select

        Label5.Text = lngFreeSpace.ToString()
        Label6.Text = lngTotalSpace.ToString()
    End Sub
    Private Sub FindDrives()
        For Each drive As String In System.Environment.GetLogicalDrives()
            
            ComboBox1.Items.Add(drive)
            Next
            ComboBox1.SelectedIndex = 0
    End Sub
End Class
bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Try using the event SelectedValueChanged instead of SelectionChangeCommitted for the combobox. And I'll remind you, you don't need an "Exit Select" in your case clauses.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

Tried changing the event,,still doesn't work..

bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

What results are you getting? Saying "it doesn't work" doesn't really help me narrow it down. There are 1001 way for it not to work.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

i am still not even getting the drives listed

bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

What happens when you run the exact module I posted above?

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

It is just reading the root directory or the C drive...I want to read the logical and the mounted drives too.

bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

My sample DID enumerate all drives. My drive F is a Virtual Clone (mounted) drive. To enumerate drives, it doesn't make sense to go below the root directory.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

Actually I want my program to just read the mounted drive...help me with that please ..

bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

My code does that. What exactly do you mean by a mounted drive? Is this different from my mounted iso in my example? How are you mounting it?

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 
bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

Basically like a subst. You can't get the size of a logical drive such as that because the numbers wouldn't make sense. Free space on a drive only makes sense when the drive is physical. Let's say you have a drive letter (M:) mapped to

D:\Temp\My Folder\subdir\temp\settings

What does "free space" on M really mean when M is not really a drive? The only number that makes sense is the free space on D:.

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

I just checked further and I see I was mistaken about the mounting. As I understand it, you have a physical drive (or partition) mounted as a folder in an existing drive. May I ask why you chose to access the drive that way rather than assigning it a drive letter?

Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

Actually,I am given a task of reading a logical drive on a server which i cannot realise on my laptop..So i was just trying to draw a parallel this way

bigzos
Junior Poster in Training
61 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You