+ Reply to Thread
Results 1 to 6 of 6

Thread: Getting info about drives - VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Getting info about drives - VB.NET

    There exists many programs which will show you an overview of your drives on your computer so why learn how to do your own to be able to add what functionalities you want? I will in this tutorial how you can get a list of all drives and then get info about them all.


    Firstly we want to get an array of all the drives on the computer. To do this we have to get info about the computer's file system. The array of drives can be received like this:


    [highlight=VB.NET]My.Computer.FileSystem.Drives[/highlight]


    So know we'll get an array of the data type System.IO.DriveInfo. To get a single drive you have to use its index but we won't do that since we want to get them all and then it will be easier to use a for each loop, like this:


    [highlight=VB.NET]For Each Drive In My.Computer.FileSystem.Drives

    Next[/highlight]


    So now we'll get the System.IO.DriveInfo variable with the current drive. We can then use Drive.Name to receive its name (Like C:\ or D:\ for example) and its type(for example CDRom, fixed and removable) by using Drive.DriveType. We'll use them to get some generally info about the drives, I'll then add all drives to a listbox just to show them all, of course you can do whatever you want with the info you get here. So now the code will look like this:


    [highlight=VB.NET] For Each Drive In My.Computer.FileSystem.Drives

    Dim DriveInfo As String = Drive.Name & " (" & Drive.DriveType.ToString & ") "

    lstDrivers.Items.Add(DriveInfo)

    Next[/highlight]
    lstDrivers is the name of the listbox.



    Now the list could look something like this:
    Getting info about drives - VB.NET-drives1.png





    But we also probably want to know how big the drives are and how much space that have been used on them. Before we can do this we need to check if the drive is ready (which means it is a working drive and not a empty spot) by using Drive.IsReady. The reason we have to check this is because if we try to get the size of a drive that isn't ready we'll receive an error telling us something like: "the driver is not ready". To then get the total size we use Drive.TotalSize and we can then use Drive.TotalSize - Drive.TotalFreeSpace to get the used space. So now our code should look something like this:




    [highlight=VB.NET] For Each Drive In My.Computer.FileSystem.Drives



    Dim DriveInfo As String = Drive.Name & " (" & Drive.DriveType.ToString & ") "


    If Drive.IsReady = True Then
    DriveInfo &= Drive.TotalSize - Drive.TotalFreeSpace & "B/" & Drive.TotalSize & "B"
    Else
    DriveInfo &= "Not available"
    End If



    lstDrivers.Items.Add(DriveInfo)



    Next[/highlight]



    Which will result in a list similar to this one:

    Getting info about drives - VB.NET-drives2.png


    But now we'll see a problem, we can't really make out the size since they are in bytes, to solve this we have to convert it with a good prefix. To do this we'll add a function called setPrefix:



    [highlight=VB.NET] Private Function setPrefix(ByVal size As Long) As String


    Dim TotalString As String = ""

    For Prefix As Integer = 0 To 4
    If size < 1024 Or Prefix = 4 Then
    TotalString = size
    Select Case Prefix
    Case 0
    TotalString &= "B"
    Case 1
    TotalString &= "KB"
    Case 2
    TotalString &= "MB"
    Case 3
    TotalString &= "GB"
    Case 4
    TotalString &= "TB"
    End Select
    Exit For
    Else
    size /= 1024
    End If
    Next

    Return TotalString

    End Function
    [/highlight]

    So what the above code do is simply first trying if we have more or equals to one kilobyte, if we hasn't we return the amount of bytes, but if we had we divide the amounts of bytes with 1024 to get the amount of kilobytes, then we test if we have more or equals to one Megabyte etc. The biggest prefix this can return is Terra byte. So now when we have this function we should add it to our "main code" so it will look like this:


    [highlight=VB.NET] For Each Drive In My.Computer.FileSystem.Drives



    Dim DriveInfo As String = Drive.Name & " (" & Drive.DriveType.ToString & ") "


    If Drive.IsReady = True Then
    DriveInfo &= setPrefix(Drive.TotalSize - Drive.TotalFreeSpace) & "/" & setPrefix(Drive.TotalSize)
    Else
    DriveInfo &= "Not available"
    End If



    lstDrivers.Items.Add(DriveInfo)



    Next[/highlight]


    And now we'll get a list looking more like this:


    Getting info about drives - VB.NET-drives3.png



    So now we've created a program which gives us an overview of all the drives on the computer, now you can change it a bit to fit your preference. Hope you liked this tutorial

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Getting info about drives - VB.NET

    Nice work, Vswe! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Getting info about drives - VB.NET

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Sep 2010
    Location
    Yangon, Myanmar
    Posts
    7
    Rep Power
    0

    Re: Getting info about drives - VB.NET

    thanks

  6. #5
    Jarryd's Avatar
    Jarryd is offline Learning Programmer
    Join Date
    Sep 2010
    Location
    Queensland, Australia
    Posts
    63
    Rep Power
    6

    Re: Getting info about drives - VB.NET

    Nice tutorial man, I've been looking for something liek this.

  7. #6
    Niels is offline Newbie
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Re: Getting info about drives - VB.NET

    The function :
    setPrefix(drive.TotalSize - drive.TotalFreeSpace) shows how much you have used, not the free space!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Delphi] List Drives And Their Types
    By LuthfiHakim in forum Classes and Code Snippets
    Replies: 0
    Last Post: 11-22-2010, 04:57 AM
  2. Virtual Drives on Windows XP
    By manux in forum Software Development Tools
    Replies: 3
    Last Post: 04-22-2010, 07:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts