How to get available FreeSpace from a disk from a remote computer?

I am trying to get FreeSpace from drive D of a remote computer.

Towards the end, I get a ManagementException that was raw user code "Not Found"

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

Here is 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); } 
+4
source share
2 answers

I found out what the problem is.

My request was incorrect. I replaced it with:

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

And the problem is solved.

+2
source

I believe this post should have an answer for you!

Basically, it comes down to one of two ways:

Either you import GetDiskFreeSpaceEx and use it on the drive path, or use WMI on a network drive that you map in advance.

Therefore, you need to have network access for this drive.

If you are trying to control a remote system, you can easily create a small application / service that will run on this computer and constantly collect the necessary information and then provide it to the monitoring application using something like WCF or even directly 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 first. However, I will leave this answer so that anyone who finds this through a search can find it useful.

0
source

Source: https://habr.com/ru/post/1436385/


All Articles