5

I am trying to nail down free space on a remote server by querying all the drives and then looping until I find the drive I am seeking.
Is there a better way to do this?

Dim oConn As New ConnectionOptions
Dim sNameSpace As String = "\\mnb-content2\root\cimv2"
Dim oMS As New ManagementScope(sNameSpace, oConn)

Dim oQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")
Dim oSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(oMS, oQuery)

Dim oReturnCollection As ManagementObjectCollection = oSearcher.Get()
Dim oReturn As ManagementObject

For Each oReturn In oReturnCollection
    'Disk name
    Console.WriteLine("Name : " + oReturn("Name").ToString())
    'Free Space in bytes
    Dim sFreespace As String = oReturn("FreeSpace").ToString()
    If Left(oReturn("Name").ToString(), 1) = "Y" Then
        Console.WriteLine(sFreespace)
    End If
Next
Jimi
  • 29,621
  • 8
  • 43
  • 61
Jake Hackl
  • 415
  • 1
  • 4
  • 9
  • 2
    FWIW, .Net Style Guildlines specifically recommend against using prefixes like 'o' and 's'. That's a change since the VB6 days. – Joel Coehoorn Sep 11 '08 at 15:17
  • If want to use Powershell instead of VB.NET you can find one line answers at http://stackoverflow.com/questions/12159341/powershell-how-to-get-disk-capacity-and-free-space-of-remote-computer – Greg Bray Sep 17 '12 at 21:23

1 Answers1

9

Why not just make your WMI query only pull back where name='Y'?

So:

Dim oQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3 AND name='Y'")
EBGreen
  • 36,735
  • 12
  • 65
  • 85