查询远程服务器上磁盘空间的最佳方法

时间:2008-09-11 14:40:12

标签: vb.net wmi

我试图通过查询所有驱动器来确定远程服务器上的可用空间,然后循环直到找到我正在寻找的驱动器。
有一个更好的方法吗?

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

1 个答案:

答案 0 :(得分:9)

为什么不让你的WMI查询只撤回name ='Y'?

所以:

Dim oQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3 AND name='Y'")