Display available drive space - VB 2008

Associate
Joined
9 Sep 2008
Posts
40
Location
Moira, East Midlands
I admit defeat! :mad:

I am using Visual Basic 2008 - and after around 4-5 hours of searching every nook and cranny of the net I cannot for the life of me find the right solution.

I want to display the available space of a hard drive in a text box. Simple! :rolleyes:

I have lots of code (mostly VB6 - I'm asking myself why I went with 2008), such as this taken from VB 2008 help online:

Imports System
Imports System.IO

Class Test
Public Shared Sub Main()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()

Dim d As DriveInfo
For Each d In allDrives
Console.WriteLine("Drive {0}", d.Name)
Console.WriteLine(" File type: {0}", d.DriveType)
If d.IsReady = True Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine( _
" Available space to current user:{0, 15} bytes", _
d.AvailableFreeSpace)

Console.WriteLine( _
" Total available space: {0, 15} bytes", _
d.TotalFreeSpace)

Console.WriteLine( _
" Total size of drive: {0, 15} bytes ", _
d.TotalSize)
End If
Next
End Sub
End Class


And i get.....nothing!!

I have vbscript that performs perfectly well, but visual basic won't run the vbscript file (this is why I use a Mac!!) ;)

I'd like this function to run as soon as the form loads.... well actually I'd like a bottle of wine and a night out, but hey ho!

Anybody, somebody.... lend me your ears.
 
Your code works fine for me:
Drive C:\
File type: Fixed
Volume label:
File system: NTFS
Available space to current user: 48628908032 bytes
Total available space: 48628908032 bytes
Total size of drive: 107374178304 bytes
etc etc for all the other drives.

What exactly is it giving for you? Any exceptions?
 
Just a blank form?

I'm guessing I'm compiling it wrong somehow. I was under the impression this would throw up a window like the MsgBox function.
 
Just a blank form?

I'm guessing I'm compiling it wrong somehow. I was under the impression this would throw up a window like the MsgBox function.

It's writing to the console, so you need to look there for the output.
Either create it as a console app, or look in the Output window in Visual Studio for the text.

If you want it to display in a message box then you'll need to create the text as a string and then pass that into the MessageBox.Show method.
 
All I need is to get the available space of C:\, in MB, displayed into a text box. I don't need any drive name, type, label etc.

Getting the details put there though is a bloody nightmare!

I'm very new to VB so excuse my ignorance - but I don't see any console window or output window?
 
If that's all you want then you can simply replace all the stuff you have with this line:

Code:
MessageBox.Show(New DriveInfo("C").AvailableFreeSpace / Math.Pow(1024, 2))

By default it gives the value in bytes so I'm dividing by 1024^2 to get it in MB as you want.
If you want it in a textbox use the .ToString method to convert the result to a string and then assign it to the .Text property of a textbox.
 
Back
Top Bottom