By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

How to use WMI to get remote computer's directory size?

P: n/a
How can I use WMI with VB.NET to get a remote computers directory size?
How can I use a unc_path within WMI? Previously I used FileInfo and
DirectoryInfo but this method takes too long because some of the
directories are over 30GB's in size. Please help!

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Von:
Here is a peice of code. The parameter to pass is the servername (hostname)
and will get you basic stat need for storage management. It is in C# and you
can easily convert it to VB.Net in minutes.

private void CalculateFreeUsed(string srvname)

{
try

{

// Connection credentials to the remote computer -

// not needed if the logged in account has access

ConnectionOptions oConn = new ConnectionOptions();

// oConn.Username = "JohnDoe";

// oConn.Password = "JohnsPass";

string strNameSpace = @"\\";

if (srvname != "")

strNameSpace += srvname;

else

strNameSpace += ".";

strNameSpace += @"\root\cimv2";
System.Management.ManagementScope oMs = new
System.Management.ManagementScope(strNameSpace, oConn);

//get Fixed disk stats

System.Management.ObjectQuery oQuery = new
System.Management.ObjectQuery("select FreeSpace,Size,Name from
Win32_LogicalDisk where DriveType=3");

//Execute the query

ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMs,oQuery);
//Get the results

ManagementObjectCollection oReturnCollection = oSearcher.Get();

//loop through found drives and write out info

foreach( ManagementObject oReturn in oReturnCollection )

{

// Disk name

//Console.WriteLine("Name : " + oReturn["Name"].ToString());
// Free Space in bytes

strFreespace = oReturn["FreeSpace"].ToString();

D_Freespace = D_Freespace + System.Convert.ToDouble(strFreespace);

// Size in bytes

strTotalspace = oReturn["Size"].ToString();

D_Totalspace = D_Totalspace + System.Convert.ToDouble(strTotalspace);
}

}

catch

{

MessageBox.Show("Failed to obtain Server Information. The node you are
trying to scan can be a Filer or a node which you don't have administrative
priviledges. Please use the UNC convention to scan the shared folder in the
server","Server Error",MessageBoxButtons.OK, MessageBoxIcon.Error) ;

}

}

Hope this helps

Yonas

"Von Thep via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in message
news:1c******************************@DotNetMonste r.com...
How can I use WMI with VB.NET to get a remote computers directory size?
How can I use a unc_path within WMI? Previously I used FileInfo and
DirectoryInfo but this method takes too long because some of the
directories are over 30GB's in size. Please help!

--
Message posted via http://www.dotnetmonster.com

Nov 21 '05 #2

P: n/a
Thanks for the tip Yonas. One more question do I need an Assembly
reference or a namespace I need to use?

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #3

P: n/a
yes, the following reference:

System.Management

Yonas

"Von Thep via DotNetMonster.com" <fo***@DotNetMonster.com> wrote in message
news:1d******************************@DotNetMonste r.com...
Thanks for the tip Yonas. One more question do I need an Assembly
reference or a namespace I need to use?

--
Message posted via http://www.dotnetmonster.com

Nov 21 '05 #4

P: n/a
I feel like I'm so close but still no results. I'm getting an invalid
parameter error.

Here's what I have:

[code]
Imports System
Imports System.Management

Module Module1

Sub Main()
CalculateFreeUsed("\\MyServer\ShareA")
End Sub
Sub CalculateFreeUsed(ByVal srvname As String)

Try

Dim oConn As ConnectionOptions = New ConnectionOptions

Dim strNameSpace As String

If srvname <> "" Then
strNameSpace += srvname
Else
strNameSpace += "."
strNameSpace += "\root\cimv2"
End If

Dim oMs As New ManagementScope(strNameSpace, oConn)
Dim oQuery As New ObjectQuery("select FreeSpace,Size,Name from
Win32_LogicalDisk where DriveType=3")

'Execute the query
'Get the results
Dim oReturnCollection As ManagementObjectCollection
Dim search As New ManagementObjectSearcher(oMs, oQuery)
' Display each entry for Win32_processor

Dim info As ManagementObject

For Each info In search.Get()

Dim strFreespace As String = info("FreeSpace").ToString()
Dim D_Freespace As Integer = D_Freespace + System.Convert.ToDouble
(strFreespace)
Console.WriteLine(D_Freespace & vbCrLf)
'Size in bytes
Dim strTotalspace As String = info("Size").ToString()
Dim D_Totalspace As Integer = D_Totalspace + System.Convert.ToDouble
(strTotalspace)
Next
Catch ex As Exception
Console.WriteLine(ex.Message)

End Try

End Sub

End Module

[\code]

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.