How do I get the available FreeSpace from a drive from a remote computer?

心已入冬 提交于 2019-12-12 11:20:10

问题


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);
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12603641/how-do-i-get-the-available-freespace-from-a-drive-from-a-remote-computer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!