4

I'm trying to get the FreeSpace from the D drive of a remote computer.

Towards the end, I'm getting a ManagementException was unhandled by user code "Not Found"

This is the line that gives me the error: fs = m["FreeSpace"].ToString();

Here's my code:

ConnectionOptions oConn = new ConnectionOptions();

oConn.Username = "username";
oConn.Password = "password";
oConn.Authority = "ntlmdomain:XXX";

ManagementScope scope = new ManagementScope("\\\\Remote_Computer\\root\\CIMV2", oConn);

scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT DeviceID, VolumeName FROM Win32_LogicalDisk where DeviceID = 'D:'");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject m in queryCollection)
{
      //error happens here
      fs = m["FreeSpace"].ToString();

      freeSpace = Convert.ToInt64(fs);
}

2 Answers 2

2

I found out what the issue was.

My query was wrong. I replaced it with:

"SELECT FreeSpace FROM Win32_LogicalDisk where DeviceID = 'D:'"

And problem solved.

0

I believe that this post should have an answer for you!!

Basically, it boils down to one of two methods:

Either you import GetDiskFreeSpaceEx and use it on the path to the drive, or you use WMI on a network drive that you connect beforehand.

So you need to have network access to this drive.

If you're trying to monitor a remote system, you could easily create a small app/service that will run on that machine and continually collect the needed information, then provide it to the monitoring application using something like WCF or even direct connection if you want.

Let me know if this was helpful,

Max

EDIT: Actually, I misunderstood your question. I thought you were looking for a way to connect in the first place. I'll leave this answer here, though, so that anyone finding this through a search, would maybe find it useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.