Ghanisen
asked on
Determine available disk space
Hi,
Before creating a new database I'd like to check if there's enough available disk space in the drive of the directory selected by the user to create the database in.
I have a sub that determines the root of the directory selected by the user.
I have have found in MSDN the following code to return the available disk space:
Public Function GetDiskSpace() As System.UInt64
Dim diskClass As _
New System.Management.Manageme ntClass("W in32_Logic alDisk")
Dim disks As System.Management.Manageme ntObjectCo llection = _
diskClass.GetInstances()
Dim disk As System.Management.Manageme ntObject
Dim space As System.UInt64
For Each disk In disks
If CStr(disk("Name")) = "C:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
Next disk
Return space
End Function
However an exception is raised saying that "The property requested ("Name") does not exist".
They do say in MSDN that such an exception may be raised but they don't say why and what to do about it!
Any help shall be appreciated from the VB.NET beginner that I am.
Before creating a new database I'd like to check if there's enough available disk space in the drive of the directory selected by the user to create the database in.
I have a sub that determines the root of the directory selected by the user.
I have have found in MSDN the following code to return the available disk space:
Public Function GetDiskSpace() As System.UInt64
Dim diskClass As _
New System.Management.Manageme
Dim disks As System.Management.Manageme
diskClass.GetInstances()
Dim disk As System.Management.Manageme
Dim space As System.UInt64
For Each disk In disks
If CStr(disk("Name")) = "C:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
Next disk
Return space
End Function
However an exception is raised saying that "The property requested ("Name") does not exist".
They do say in MSDN that such an exception may be raised but they don't say why and what to do about it!
Any help shall be appreciated from the VB.NET beginner that I am.
ASKER
hi mnasman,
I'm using VS2003 and Framework version 1.1 and the code above raises an exception about the "disk(Name)".
Another issue is that I don't want to scan all the drives to get their available space.
I want to determine available disk space for a drive of my choice, e.g. "c:\"
Hope someone can help.
Thanks for your contribution.
I'm using VS2003 and Framework version 1.1 and the code above raises an exception about the "disk(Name)".
Another issue is that I don't want to scan all the drives to get their available space.
I want to determine available disk space for a drive of my choice, e.g. "c:\"
Hope someone can help.
Thanks for your contribution.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi mnasman,
I do get the drive size but only for the "c:" drive as indicated in the code above. I get it also for my "d:" drive if I replace "c:" by "d:" in the code above.
My problem is that I don't know which drive the user will select. I have a sub that finds out what drive he has selected and the name of that drive (e.g. "D:") is returned in a variable named repName.
I'd like to be able to replace "C:" in ( CStr(disk("Name")) = "C:" ) by (CStr(disk("Name")) = " & repName &") or something of the sort.
I noticed also a huge difference betwween the drive size given by this function and what windows explorer gives for my C drive :
Function : 92,555,206,656 bytes
Windows Explorer : 86,1 Gbytes
That means a difference of about 6 Gbytes!
I hope you've got something to propose.
Thanks for trying
I do get the drive size but only for the "c:" drive as indicated in the code above. I get it also for my "d:" drive if I replace "c:" by "d:" in the code above.
My problem is that I don't know which drive the user will select. I have a sub that finds out what drive he has selected and the name of that drive (e.g. "D:") is returned in a variable named repName.
I'd like to be able to replace "C:" in ( CStr(disk("Name")) = "C:" ) by (CStr(disk("Name")) = " & repName &") or something of the sort.
I noticed also a huge difference betwween the drive size given by this function and what windows explorer gives for my C drive :
Function : 92,555,206,656 bytes
Windows Explorer : 86,1 Gbytes
That means a difference of about 6 Gbytes!
I hope you've got something to propose.
Thanks for trying
ASKER
Hi mnasman
I found a solution which is not very elegant (I suppose I can improve it later with Case ) :
Public Function GetDiskSpace() As System.UInt64
Dim di As DirectoryInfo
di = New DirectoryInfo(CheminChoisi )
Dim RepName As String
RepName = di.Root.ToString.TrimEnd(" \")
Dim diskClass As New System.Management.Manageme ntClass("W in32_Logic alDisk")
Dim disks As System.Management.Manageme ntObjectCo llection = diskClass.GetInstances()
Dim disk As System.Management.Manageme ntObject
Dim space As System.UInt64
For Each disk In disks
If RepName = "C:" Then
If CStr(disk("Name")) = "C:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "D:" Then
If CStr(disk("Name")) = "D:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "E:" Then
If CStr(disk("Name")) = "E:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "F:" Then
If CStr(disk("Name")) = "F:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "G:" Then
If CStr(disk("Name")) = "G:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "H:" Then
If CStr(disk("Name")) = "H:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
End If
Next
Return space
End Function
It works fine.
Anyway you get the 500 points because you tried to help and actually at least encouraged me to keep trying since the MSDN code worked for you.
Thanks.
I found a solution which is not very elegant (I suppose I can improve it later with Case ) :
Public Function GetDiskSpace() As System.UInt64
Dim di As DirectoryInfo
di = New DirectoryInfo(CheminChoisi
Dim RepName As String
RepName = di.Root.ToString.TrimEnd("
Dim diskClass As New System.Management.Manageme
Dim disks As System.Management.Manageme
Dim disk As System.Management.Manageme
Dim space As System.UInt64
For Each disk In disks
If RepName = "C:" Then
If CStr(disk("Name")) = "C:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "D:" Then
If CStr(disk("Name")) = "D:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "E:" Then
If CStr(disk("Name")) = "E:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "F:" Then
If CStr(disk("Name")) = "F:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "G:" Then
If CStr(disk("Name")) = "G:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
ElseIf RepName = "H:" Then
If CStr(disk("Name")) = "H:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
End If
Next
Return space
End Function
It works fine.
Anyway you get the 500 points because you tried to help and actually at least encouraged me to keep trying since the MSDN code worked for you.
Thanks.
Your code worked fine with me, I just copy and paste and that's work
Are you using Vs2002 or VS2003?, and .net framework that installed 1.0 or 1.1?
maybe this code require framework 1.1